Valks-Games/io-game

Unknown Networking Fault

Closed this issue · 2 comments

client.js:163 Uncaught TypeError: Cannot read property 'updateMessage' of undefined
    at Socket.<anonymous> (client.js:163)
    at Socket.Emitter.emit (index.js:138)
    at Socket.onevent (socket.js:282)
    at Socket.onpacket (socket.js:241)
    at Manager.<anonymous> (index.js:23)
    at Manager.Emitter.emit (index.js:138)
    at Manager.ondecoded (manager.js:350)
    at Decoder.<anonymous> (index.js:23)
    at Decoder.Emitter.emit (index.js:138)
    at Decoder.add (index.js:256)

I've added a undefined check to suppress it and prevent crash for client. Not tested.

game.socket.on('messages', function(data) {
  if (data.id == game.player.id) {
    game.player.updateMessage(data.text);
  } else {
    // Should we delete the player at game.players[data.id] here? Or just check if its not undefined?
    if (game.players[data.id] != undefined)
      game.players[data.id].updateMessage(data.text);
  }
});
aeuu commented

Just a note, it is a bit cleaner to do this instead:

game.socket.on('messages', function(data) {
  if (data.id == game.player.id)
    return void game.player.updateMessage(data.text); // Will run this, then exit the function if this if statement is run

  if (!game.players[data.id]) // returns if it is undefined
    return;

  game.players[data.id].updateMessage(data.text);
});

(It does the same thing, looks a bit nicer though)

Unsure of how to reproduce this error so I'll close this issue for now as it has not reappeared in a while. May have fixed it along the way without even knowing it.