node.js - Use socket.get when disconnect event is fired -
i'm making little game. when client disconnects, want others clients notified, delete user in client-side.
socket.on('disconnect', function () { socket.get('player', function (error, player) { console.log(player.pseudo + ' disconnected.'); socket.broadcast.emit('removeplayer', player.pseudo); }); });
sometimes, error:
console.log(player.pseudo + ' disconnected.'); ^ typeerror: cannot read property 'pseudo' of null
is because player
removed ? way player's username ?
edit: oh got it. bug appened when user hadn't sent pseudo. so, player.pseudo
wasn't set. check first if player known.
socket.on('disconnect', function (disconnect) { socket.get('player', function (error, player) { if (player) { console.log(player.pseudo + ' disconnected.'); socket.broadcast.emit('removeplayer', player.pseudo); } }); });
Comments
Post a Comment