Is it possible to get the socket.id
of the client that has disconnected? The following code gives me undefined
for socket.id
Node.js Code
io.sockets.on('connection', function() {
socket.on('disconnect', function(socket) {
console.log(socket.id);
});
});
The callback function that
io.sockets.on
takes as its second argument is supposed to take one argument: the socket. Yours doesn't, so the socket on the second line'ssocket.on
is undefined.And the callback for
socket.on
isn't given any arguments, so thesocket
in that function is also undefined.The code should work if you move the socket parameter from the second function declaration to the first: