apscience
apscience

Reputation: 7253

Socket.io auto disconnect after 2 hours of inactivity not working

I'd like to disconnect users from socket.io after 2 hours of inactivity and give them a prompt / reconnect button.

Firstly, I have set auto reconnect to false and a variable forcedc:

var socket = io.connect('https://coinchat.org:443',{secure: true, reconnect: false});
var forcedc = false;

Next, I have a time out for inactivity that is reset when someone moves the mouse:

$("body").mousemove(function(e){
    clearTimeout(dcTimeout);
    dcTimeout = setTimeout(function(){
        if(!forcedc){
            forcedc = true;
            socket.disconnect();
            $("#dcmodal").modal('show');
        }
    }, 1000 * 60 * 60 * 2);
});

Basically, forcedc means that the user was disconnected due to inactivity and the browser should not refresh the page to try and reconnect. If forcedc is false, it means that the user was disconnected due to network issues / server restarts, and the browser should refresh the page to reconnect.

Now, when the socket is disconnected:

socket.on("disconnect", function(data){
    if(!forcedc){
        setTimeout(function(){document.location.reload(true)}, 1000 + Math.random()*12750);
    }
});

The page should refresh if forcedc is false. However, the page refreshes even if the user has already been "disconnected" due to inactivity (I know forcedc = true has been executed, as I see the disconnected modal).

What am I doing wrong?

Upvotes: 1

Views: 3167

Answers (3)

neowulf33
neowulf33

Reputation: 645

I believe that dcTimeout never gets cleared via 'clearTimeout(dcTimeout);". Hence, dcTimeout gets executed and forcedc becomes true.

Possible solution: declare dcTimeout as a global variable along with forcedc. Here's a jsfiddle link with my test code for your reference.

function communication() {  

  var socket = io.connect('http://localhost:8080',{reconnect: false});
  socket.on('connect', function () {

      var forcedc = false;
      var dcTimeout = null;

      console.log("connected to the server!");

      socket.on('news', function (data) {

        console.log(data);
      });

      socket.on("disconnect", function(data){

          console.log("disconnected..")
          if(!forcedc){
              console.log("forcedc is false and the socket has been disconnected");
              setTimeout(function(){
                  console.log("Reloading the document in 2 seconds");
                  //document.location.reload(true)
              }, 2000);
          }
      });

      document.onmousemove = function(e){

          console.log("mouse movement detected");
          clearTimeout(dcTimeout);
          dcTimeout = setTimeout(function(){
              if(!forcedc){
                  forcedc = true;
                  console.log("disconnecting...setting to true");
                  socket.disconnect();
                  //$("#dcmodal").modal('show');
                  console.log("Disconnected! Do you want to reconnect?");
              }
          }, 5000 * 1);
      }
  });
}

Upvotes: 1

Sebastien C.
Sebastien C.

Reputation: 2109

Without any more detail it's hard to answer. Is there another place where you use forcedc on the code? One thing you could try is to put it in an object : namespace.forcedc = true to force closure inheritance

Upvotes: 0

Sebastien C.
Sebastien C.

Reputation: 2109

I guess it has to be with the closures... Where are you declaring forcedc? and where are those 2 pieces of code in your whole code?

Apparently the forcedc variables doesn't update as they should, which could mean one of them is a local copy

Upvotes: 0

Related Questions