I've recently set-up a local WebSocket server which works fine, however I'm having a few troubles understanding how I should handle a sudden loss of connection which neither the client or server intentionally initiated, i.e: Server loses power, ethernet cables pulled out etc... I need the client's to know whether connection has been lost within ~10seconds.
Client side, connection is simply:
var websocket_conn = new WebSocket('ws://192.168.0.5:3000'); websocket_conn.onopen = function(e) { console.log('Connected!'); }; websocket_conn.onclose = function(e) { console.log('Disconnected!'); };
I can manually trigger the connection disconnect which works fine,
websocket_conn.close();
But if I simply pulled the ethernet cable out the back of the computer, or disabled the connection, onclose
doesn't get called. I've read in another post that it would eventually get called when TCP detects loss of connectivity, but it's not in the timely manner that I need as the default for Firefox I believe is 10 minutes, and I don't really want to go around hundreds of computers about:config
changing this value. The only other suggestion I've read is to use a 'ping/pong' keep-alive polling style method which seems counterintuitive to the idea of websockets.
Is there an easier way to detect this kind of disconnect behaviour? Are the old posts i'm reading still up to date from a technical point, and the best method is still 'ping/pong' style?
In order to communicate using the WebSocket protocol, you need to create a WebSocket object; this will automatically attempt to open the connection to the server. The URL to which to connect; this should be the URL to which the WebSocket server will respond.
The basic idea is to enable a bi-directional communcation between client and server, without the need to oepning multiple http connections (e.g., long polling). The Websocket only defines the protocol on the wire and allow you to choose the application level protocol by Sec-WebSocket-Protocol .
In some cases, however, the client or the server may not be aware that the network connection has become stale. For example, if a network cable is unplugged, a network interface is shut down, or an intermediate component in the connection (such as a proxy server) becomes disconnected.
With at least 30 GiB RAM you can handle 1 million concurrent sockets.
Create a code in server when receive __ping__ send __pong__ back
JavaScript code is give below
function ping() { ws.send('__ping__'); tm = setTimeout(function () { /// ---connection closed /// }, 5000); } function pong() { clearTimeout(tm); } websocket_conn.onopen = function () { setInterval(ping, 30000); } websocket_conn.onmessage = function (evt) { var msg = evt.data; if (msg == '__pong__') { pong(); return; } //////-- other operation --// }
This was the solution I ended up doing which seems to work fine for the time being, it's entirely specific to my project's setup & relies on criteria being in place that wasn't originally mentioned in my question, but it might be useful for someone else if they happen to be doing the same thing.
The connection to the websocket server occurs within a Firefox addon, and by default Firefox's TCP setup has a 10 minute timeout. You can see additional details with about:config
and searching for TCP.
Firefox addons can access these parameters
var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
and also change these parameters by specifying the branch & preference along with the new value
prefs.getBranch("network.http.tcp_keepalive.").setIntPref('long_lived_idle_time', 10);
So now, any computer with the addon installed have a 10 second timeout for TCP connections. If the connection is lost, the onclose
event is triggered which displays an alert and also attempts to re-establish connection
websocket_conn.onclose = function (e) { document.getElementById('websocket_no_connection').style.display = 'block'; setTimeout(my_extension.setup_websockets, 10000); };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With