Skip to content

Commit 39e1438

Browse files
committed
Added option for maxReconnectInterval
Added an extra option to specify the maximum amount of time to wait before attempting a reconnect. Defaults to -1, to keep the current behaviour.
1 parent ee8f794 commit 39e1438

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

reconnecting-websocket.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@
104104
debug: false,
105105
/** The number of milliseconds to delay before attempting to reconnect. */
106106
reconnectInterval: 1000,
107+
/** The maximum number of milliseconds to delay a reconnection attempt. */
108+
maxReconnectInterval: -1,
107109
/** The rate of increase of the reconnect delay. Allows reconnect attempts to back off when problems persist. */
108110
reconnectDecay: 1.5,
109111
/** The maximum time in milliseconds to wait for a connection to succeed before closing and retrying. */
@@ -232,10 +234,14 @@
232234
}
233235
eventTarget.dispatchEvent(generateEvent('close'));
234236
}
237+
var timeout = self.reconnectInterval * Math.pow(self.reconnectDecay, self.reconnectAttempts);
238+
if (self.maxReconnectInterval != -1 && timeout > self.maxReconnectInterval)
239+
timeout = self.maxReconnectInterval;
240+
235241
setTimeout(function() {
236242
self.reconnectAttempts++;
237243
connect(true);
238-
}, self.reconnectInterval * Math.pow(self.reconnectDecay, self.reconnectAttempts));
244+
}, timeout);
239245
}
240246
};
241247
ws.onmessage = function(event) {

0 commit comments

Comments
 (0)