Akram Kamal Qassas
Akram Kamal Qassas

Reputation: 509

Disconnect socket.io and send the disconnect reason

I need to disconnect some users from server using this code:

socket.disconnect('User disconnected because of ....');

but when i handle the disconnect event on user side i don't see the disconnect reason

socket.on('disconnect', function(reason){
console.log('User 1 disconnected because '+reason);
}); 

I am using node for server side and browsers as client side, Any idea how to send a disconnect reason?

Upvotes: 1

Views: 2136

Answers (1)

Mark
Mark

Reputation: 36

It seem there is no way to do such a thing like that but you can do the trick using emit function before closing the socket

Code in server side:

socket.emit('closeReason','User disconnected because of ....');
socket.disconnect();

Code in client side:

socket.on('closeReason',function(reason){ ... });

Upvotes: 2

Related Questions