javascriptnode.jsdom-eventsdisconnect

Make client send disconnect event on tab close node.js


I'm making a multiplayer game with a node.js server with socket.io and a html5 canvas with a js file in it for client.

Now I have the socket.on('disconnect',function(){my code here}); on server side, but when a user closes the tab the client is running in it doesn't trigger any disconnect event in the server, but I need the server to notice when a user closes the tab.

How can I make the client trigger the event correctly when one closes the tab it's running in?


Solution

  • I don't think, that it's possible. So I would suggest to have a timeout for each socket connection on the server to close it automatically. On the client side, you could send a "ping" to keep the connection open, when nothing happens. ;) How about that?