NodeJS Einaros WS Connection Timeout

I use the Einaros WS module with Node JS on one computer (server) , and it works fine when I connect using another computer (client) .

If I plug in a network cable, ws.on('close', function()... does not work, and I'm looking for advice:

  • I have to implement my own ping / pong or

  • There is a built-in function to handle this automatically.

+7
source share
1 answer

Einaros WS has the ability to send Ping and Pong frames, which are understood by most browsers and socket frameworks. I tested Socket Rocket on iOS and it had no problem understanding ping frames for Einaros. You need to come up with application-specific logic for how often to ping, as well as how many missed pongs you will endure. Einaros WS has ping () and pong () functions for sending ping pong or pong. You are listening to the pong event to find out when you received a response from customers. Here is how I do it in my code:

 wss.on("connection", function(ws) { console.log("websocket connection open"); ws.pingssent = 0; var interval = setInterval(function() { if (ws.pingssent >= 2) {// how many missed pings you will tolerate before assuming connection broken. ws.close(); } else { ws.ping(); ws.pingssent++; } }, 75*1000);// 75 seconds between pings ws.on("pong", function() { // we received a pong from the client. ws.pingssent = 0; // reset ping counter. }); }); 

In the case of Socket Rocket, no code is required on the client side. Therefore, compatible browsers and clients will respond Pongs automatically.

+16
source share

All Articles