diff --git a/lib/main.js b/lib/main.js index 90dc0428..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); @@ -241,6 +242,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 +263,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 +329,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.