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

Popular posts from this blog

c# - Unity IoC Lifetime per HttpRequest for UserStore -

Change the color of an oval at click in Java AWT -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -