From 285c39d777453739b54058ed27e430a3291aef80 Mon Sep 17 00:00:00 2001 From: Josef-MrBeam <81746291+Josef-MrBeam@users.noreply.github.com> Date: Thu, 15 Sep 2022 11:37:29 +0200 Subject: [PATCH 1/2] add watchdog for heartbeat --- lib/main.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/main.js b/lib/main.js index 90dc0428..68a62b81 100644 --- a/lib/main.js +++ b/lib/main.js @@ -241,6 +241,11 @@ SockJS.prototype._transportTimeout = function() { } }; +SockJS.prototype._websocketWatchdogCb = function(websocket) { + debug("watchdog websocket timeout"); + websocket._close(1006, 'Server lost session'); +} + SockJS.prototype._transportMessage = function(msg) { debug('_transportMessage', msg); var self = this @@ -257,6 +262,8 @@ SockJS.prototype._transportMessage = function(msg) { case 'h': this.dispatchEvent(new Event('heartbeat')); debug('heartbeat', this.transport); + clearTimeout(this.heartbeatWatchdog); + this.heartbeatWatchdog = setTimeout(this._websocketWatchdogCb, SockJS.WATCHDOG_WEBSOCKET_TIMEOUT, this); return; } @@ -321,6 +328,7 @@ SockJS.prototype._open = function() { this.transport = this._transport.transportName; this.dispatchEvent(new Event('open')); debug('connected', this.transport); + this.heartbeatWatchdog = setTimeout(this._websocketWatchdogCb, SockJS.WATCHDOG_WEBSOCKET_TIMEOUT, this); } else { // The server might have been restarted, and lost track of our // connection. From a3f2d942e2fc59eb5961e17a48a6525bf5f84411 Mon Sep 17 00:00:00 2001 From: Josef-MrBeam <81746291+Josef-MrBeam@users.noreply.github.com> Date: Thu, 15 Sep 2022 11:40:32 +0200 Subject: [PATCH 2/2] add timeout constant --- lib/main.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/main.js b/lib/main.js index 68a62b81..79af6ac3 100644 --- a/lib/main.js +++ b/lib/main.js @@ -172,6 +172,7 @@ SockJS.CONNECTING = 0; SockJS.OPEN = 1; SockJS.CLOSING = 2; SockJS.CLOSED = 3; +SockJS.WATCHDOG_WEBSOCKET_TIMEOUT = 30000; // The heartbeat appears every 25 seconds, so we have 5 seconds delay buffer SockJS.prototype._receiveInfo = function(info, rtt) { debug('_receiveInfo', rtt);