saoyr5
saoyr5

Reputation: 111

Socket.io firing callback function multiple times

I'm working on a multiplayer canvas game using node.js and socket.io and I'm having problems with my node server throwing a ton of the same error immediately after the client handshake is established.

Here's a copy/paste from the console:

    info  - handshake authorized GeDlfwyEdfEIJXGvoZjG
    new player connected: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG

You can check the full code here: http://github.com/storrdev/delta-wing

Basically I'm having a problem where the node console for server.js (node script) spits out "(OnMovePlayer) Player Not Found: CONNECTION ID" where CONNECTION ID is the id of the client connected.

I wrote that error message to display when the function onMovePlayer() is called when the server receives a "move player" message from the client and the player's id cannot be found in array of players stored on the server.

What I can't understand is that the server shouldn't even be invoking the onMovePlayer() function until the client emits the "move player" message to the server, and the client shouldn't emit the "move player" message until after the connection is established, and it emits a "new player" message to the server.

Why is it throwing this error so many times? Are socket.emits sent out of order? I'm so confused.

I know this is convoluted so I appreciate any help or ideas I can get. I honestly was hoping that typing the situation out would help me figure it out, but unfortunately it has not.

Upvotes: 1

Views: 1496

Answers (1)

user568109
user568109

Reputation: 48003

There is some problem in the Game.update in game.js, it seems to be calling itself recursively.

if (Game.players[0].update()) {
    //This seems to execute recursively.
    socket.emit('move player', {x: Game.players[0].getX(), y: Game.players[0].getY(), angle: Game.players[0].getAngle()});
    Game.background.update(Game.players[0].getX(), Game.players[0].getY());
}

I checked it by

count=0;
if (Game.players[0].update()) {
    socket.emit('move player', {x: Game.players[0].getX(), y: Game.players[0].getY(), angle: Game.players[0].getAngle()});
    count +=1;
    console.log(count);
    Game.background.update(Game.players[0].getX(), Game.players[0].getY());
}

I could not understand the logic by looking at the code and tell if it is right behaviour. But it keeps sending move player requests to server.

Edited

After some testing, I found that the issue is due to synchronization.

When a client loads the page, it starts emitting the move player right away, even before it connects to the server and runs onSocketConnected. The number of lines you get on the error console are equal to the number of messages sent before Connected to socket server is shown in console. Try the code I used for debugging.

So it is breaking the assumption that client shouldn't emit the "move player" until after the connection is established. Hope this helps you.

Upvotes: 1

Related Questions