I'm making a webapp with Javascript, Node.js, and socket.io. In one part of the web app (the lobby), users can press a button to start the game, which does this:
$.ajax({
url: "gameStart",
type: 'POST',
data: {myName: myNickname,
enemyName: accepter,
roomNamer: "1",
showValid: offerValidStr,
showThreat: offerThreatStr},
success: function(page) {
document.open();
document.write(page);
}
});
The server responds to that request by rendering an HTML page and sending it (along with the associated CSS and Javascript files). The user can also navigate from the game page back to the lobby, which happens in a similar manner. Both the Javascript file for the lobby and for the gameStart page have a part where they make a socket connection with:
var socket = io();
In both Javascript files, if the user is disconnected without navigating away from the page (perhaps because of server problems), I have:
socket.on('disconnect', function() {
prettyAlert("Connection Lost", "The connection has been lost. "
+ " Sorry about that! You should return to the <a href='https://toroidal-chess.herokuapp.com/'>login page</a>. "
+ "This could just be bad luck. However, if it keeps happening, "
+ " it is probably a bug.", [OK_BUTTON], true, "disconnect");
`})`;
I was under the impression that when a user navigates between the lobby and the gameStart page, any sockets they open are disconnected, and thus a new socket is created. However, when I test it locally by running a Node server and connecting to it with a browser, when I kill the server, multiple of the alert messages pop up. In fact, one pops up for every time I've been to the lobby and the gameStart page. So I guess that means that all the old sockets are still there? But I thought that the Javascript from one page stops running once the user goes to a different page. Can someone explain what's happening?