3

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?

Zanik
  • 103
  • 1
  • 9
  • Normally unless a socket is closed manually, or the user totally close the browsers/tabs of that site, will still remain open, although sometimes you should check how you coded it to work. You can code it to close the socket when the game start and open another one if you want – Ezekiel Jan 13 '18 at 10:37
  • But the document is only "cleared" when you call `document.open`, that does not mean that js in the background is stopped. And you should definetly not rewrite the page all the time. Design it as a single page where some parts are hidden. – Jonas Wilms Jan 13 '18 at 10:39

2 Answers2

2

Unless you are doing a SPA, there is no way to avoid the socket.io disconnection when an user changes/refreshes the page.

You can handle client disconnection event on server side.

Juanlu
  • 69
  • 7
1

Normally unless a socket is closed manually, or the user totally close the browsers/tabs of that site, will still remain open, although sometimes you should check how you coded it to work. You can code it to close the socket when the game start and open another one if you want .

The basic Concept there some sort of handshake between the browser and the site server, allowing flow of data,packet what ever it maybe. So starting a game of the same site which probably started on that same page, the socket will still be open, imagine like facebook you on your profile and the you open a group page does that stop your chats from coming in? No. So I hope the insight was helpful

Ezekiel
  • 671
  • 5
  • 13