From 7b77782b57d948e6c1dcf85face202bc9acc8b43 Mon Sep 17 00:00:00 2001 From: Robert Lowe Date: Mon, 12 Feb 2018 15:28:53 -0500 Subject: [PATCH] Adds heartbeat to prevent Websocket closing This change ensures Firefox (and others?) don't close connection when detected as idle with a CloseEvent of 1006. This is an opinionated change due to when using `ws.onclose` on the client. This is entirely committed for note and posterity. --- kurento-one2many-call/server.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/kurento-one2many-call/server.js b/kurento-one2many-call/server.js index 15ffca18..5bee90ce 100755 --- a/kurento-one2many-call/server.js +++ b/kurento-one2many-call/server.js @@ -74,6 +74,13 @@ function nextUniqueId() { */ wss.on('connection', function(ws) { + // prevent 1006 close on client + var heartbeat = setInterval(function(){ + // https://github.com/websockets/ws/blob/master/doc/ws.md#websocketpingdata-mask-callback + ws.ping(); + }, 3000); + + var sessionId = nextUniqueId(); console.log('Connection received with sessionId ' + sessionId); @@ -85,6 +92,7 @@ wss.on('connection', function(ws) { ws.on('close', function() { console.log('Connection ' + sessionId + ' closed'); stop(sessionId); + clearInterval(heartbeat); }); ws.on('message', function(_message) {