I am trying to prevent the client from disconnecting from the server. So before the user closes the window on which the app is open, I do:
$(window).bind("beforeunload", function() {
return("Close the app?");
});
But the problem is that no matter if the user chooses to leave or stay on the page where the app is open, the client get’s disconnected (stops listening) from the server, before even I chose an option. So if the user chooses to stay on the page, nothing will be sent or received from the server.
Why can this be? How can this be prevented?
I had exactly the same problem in my project.
When you call
socket.connect(), you should setsync disconnect on unloadparameter of your connection tofalse(it istrueby default):You can find more connection options here: https://github.com/LearnBoost/socket.io/wiki/Configuring-Socket.IO
P.S. Hope it’s still actual for you 🙂
UPD.
Since the doc was changed,
falseis now the default value ofsync disconnect on unload