Skip to content

Instantly share code, notes, and snippets.

@Nasawa
Created May 20, 2014 03:09
Show Gist options
  • Save Nasawa/73b0e4a40668f8362e50 to your computer and use it in GitHub Desktop.
Save Nasawa/73b0e4a40668f8362e50 to your computer and use it in GitHub Desktop.
My first sock.js fun
//DEBUG
sock.onmessage = function(message) //When the client hears from the server
{
var msg = JSON.parse(message.data); //parse the JSON object to var msg
try
{
if(msg.name != cName) //if this is for another player
{
if(msg.func == 'updatePosition') //if that player movied
{
// !! this could be mapped to be more efficient
for(var n = 0; n < players.length; n++) //for each player logged in
{
if(players[n]._name == msg.name) //if the player matches the player the server was talking about
{
players[n]._multitoward = msg.toward; //set the toward direction
players[n]._playertile = tilearray[msg.playertile]; //set the specific tile to aim at
}
}
}
if(msg.func == 'updateChat') //if the server said that the other player talked
{
multichat(msg.text, msg.speaker, msg.color); //call the function with the chat text, player name and player's color
}
if(msg.func == 'login') //if the server said a player logged in
{
addPlayer(msg.name); //add them to the player array. Only need the name since everything else will be dynamic.
}
if(msg.func == 'logout') //and if that player should ragequit
{
for(var i = 0; i < players.length; i++) //then client's gonna respond with a /kick
{
if(players[i]._name == msg.name)
players.splice(i, 1);
}
}
console.log("Message Name: " + msg.name + " Player Name: " + cName);
}
console.log(message);
}
catch(e){console.log(e);}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment