javascript - Detect internet disconnection via socket -


currently i'm using node.js server has tcp socket connection(using socket.io) using i'm showing real-time data via browser.

consider scenario, have logged in application , real-time shown via web browser. of sudden network got disconnected. @ show message saying "expired" when client-side socket emits disconnect event.

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

now problem: above disconnect event emitted after 60 seconds; in-between when user tries update, not work , create bad impression us.

how overcome situation , how emit disconnect event of sudden when network got disconnected?

you have understand heartbeat interval , heartbeat timeout. earlier time when server emits heartbeat each 25 seconds (by default). , later timeout server keep specific client in memory. if client receives heartbeat within heartbeat timeout (60 seconds default) tells server i'm alive. if client doesn't receive heartbeat server within timeout server flushes client out of memory , disconnect event fired automatically on client side. timeout set when client connects server. can set these properties in node.js server this:

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

now server emit heartbeat every 5 seconds , if client doesn't receive heartbeat in 10 seconds, disconnect event fired. remember heartbeat interval should less heartbeat timeout. hope work!


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -