V
V
Vitaly2017-03-04 16:24:38
JavaScript
Vitaly, 2017-03-04 16:24:38

Is my socket.io getting disconnected?

Hello. Faced with a situation when the client falls off in sockets at inactivity. Implemented pingpong, as a result, the following problem was found out. The socket is lost when the page is not active in the browser, is there any way to fix it?
This question is a repetition, therefore, for those who suffer, I will immediately give an example of a standard ping pong that I use.
Server

function sendHeartbeat(){
    setTimeout(sendHeartbeat, 8000);
    io.sockets.emit('ping', { beat : 1 });
}

io.sockets.on('connection', function (socket) {
    socket.on('pong', function(data){
        console.log("Pong received from client");
    });
}
setTimeout(sendHeartbeat, 8000);

Customer:
socket.on('ping', function(data){
      socket.emit('pong', {beat: 1});
    });

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question