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?
- 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
2 Answers
Reset to default 4You 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
版权声明:本文标题:javascript - Detect internet disconnection via socket - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745447004a2658709.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论