admin管理员组

文章数量:1426957

Currently I'm using node.js server which has TCP socket connection(using socket.io) using this I'm showing real-time data via browser.

Consider a scenario, I have logged in to my application and all real-time is shown via web browser. Now all of a sudden the network got disconnected. At that I would like to show a message saying "Expired" when the client-side socket emits the disconnect event.

socket.on('disconnect', function () {
            console.log("Disconnected from the server");
            alert("disconnect");
        });

Now my problem: the above disconnect event is emitted after 60 seconds; In-between when user tries to update, it will not work and this will create a bad impression for us.

How to overe this situation and how to emit the disconnect event all of a sudden when network got disconnected?

Currently I'm using node.js server which has TCP socket connection(using socket.io) using this I'm showing real-time data via browser.

Consider a scenario, I have logged in to my application and all real-time is shown via web browser. Now all of a sudden the network got disconnected. At that I would like to show a message saying "Expired" when the client-side socket emits the disconnect event.

socket.on('disconnect', function () {
            console.log("Disconnected from the server");
            alert("disconnect");
        });

Now my problem: the above disconnect event is emitted after 60 seconds; In-between when user tries to update, it will not work and this will create a bad impression for us.

How to overe this situation and how to emit the disconnect event all of a sudden when network got disconnected?

Share Improve this question asked Mar 6, 2014 at 7:18 PraveenPraveen 56.6k35 gold badges136 silver badges166 bronze badges 2
  • This is, in general, not possible. How could your app be notified 'all of a sudden' when some router in another country crashes? The network/protocol will try to reroute your data and this takes time. – Martin James Commented Mar 6, 2014 at 10:14
  • @MartinJames It is possible. Already when I close/refresh the browser it is triggering the "disconnect" event; whereas when I try disconnect the network cable, it is triggering the same disconnect event after 60secs. So my problem is the delay and this is due to heartBeat pulse between browser/node and socket.io which is explained clearly in the below answer. Hope you got my point :) – Praveen Commented Mar 6, 2014 at 15:07
Add a ment  | 

2 Answers 2

Reset to default 4

You have to understand heartbeat interval and heartbeat timeout. Earlier is the time when server emits a heartbeat each 25 seconds (by default). And later is timeout for server to keep that specific client in memory. If client receives the heartbeat within heartbeat timeout (60 seconds by default) it tells the server that I'm alive. If client doesn't receive a heartbeat from server within this timeout then server flushes that client out of memory and disconnect event is fired automatically on client side. This timeout is set when client connects to server. So you can set these properties in your Node.js server like this:

var socket = require('socket.io').listen(80,{
  'heartbeat interval': 5,
  'heartbeat timeout' : 10
});

Now server will emit heartbeat every 5 seconds and if client doesn't receive a heartbeat in 10 seconds, disconnect event will be fired. But remember that heartbeat interval should be less than heartbeat timeout. Hope this will work!

This is my solution

var io = require('socket.io')
  .listen(server, {'pingTimeout':4000, 'pingInterval':2000});

本文标签: javascriptDetect internet disconnection via socketStack Overflow