Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -241,6 +242,11 @@ SockJS.prototype._transportTimeout = function() {
}
};

SockJS.prototype._websocketWatchdogCb = function(websocket) {
debug("watchdog websocket timeout");
websocket._close(1006, 'Server lost session');
Comment on lines +245 to +247
Copy link
Preview

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameter name 'websocket' is misleading as it refers to the SockJS instance, not a WebSocket. This should be 'sockjs' or 'self' to accurately reflect what it represents.

Suggested change
SockJS.prototype._websocketWatchdogCb = function(websocket) {
debug("watchdog websocket timeout");
websocket._close(1006, 'Server lost session');
SockJS.prototype._websocketWatchdogCb = function(sockjs) {
debug("watchdog websocket timeout");
sockjs._close(1006, 'Server lost session');

Copilot uses AI. Check for mistakes.

}

SockJS.prototype._transportMessage = function(msg) {
debug('_transportMessage', msg);
var self = this
Expand All @@ -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);
Copy link
Preview

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The watchdog timer should be cleared when the connection is closed to prevent potential memory leaks and unexpected behavior. Consider adding clearTimeout(this.heartbeatWatchdog) in the _close method.

Copilot uses AI. Check for mistakes.

return;
}

Expand Down Expand Up @@ -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.
Expand Down