From 39e1438778ed3585c79fb000dbf4542f0412cf82 Mon Sep 17 00:00:00 2001 From: JamesThorpe Date: Tue, 16 Dec 2014 19:17:40 +0000 Subject: [PATCH 1/4] 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. --- reconnecting-websocket.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/reconnecting-websocket.js b/reconnecting-websocket.js index 4844dfd..933f049 100644 --- a/reconnecting-websocket.js +++ b/reconnecting-websocket.js @@ -104,6 +104,8 @@ debug: false, /** The number of milliseconds to delay before attempting to reconnect. */ reconnectInterval: 1000, + /** The maximum number of milliseconds to delay a reconnection attempt. */ + maxReconnectInterval: -1, /** The rate of increase of the reconnect delay. Allows reconnect attempts to back off when problems persist. */ reconnectDecay: 1.5, /** The maximum time in milliseconds to wait for a connection to succeed before closing and retrying. */ @@ -232,10 +234,14 @@ } eventTarget.dispatchEvent(generateEvent('close')); } + var timeout = self.reconnectInterval * Math.pow(self.reconnectDecay, self.reconnectAttempts); + if (self.maxReconnectInterval != -1 && timeout > self.maxReconnectInterval) + timeout = self.maxReconnectInterval; + setTimeout(function() { self.reconnectAttempts++; connect(true); - }, self.reconnectInterval * Math.pow(self.reconnectDecay, self.reconnectAttempts)); + }, timeout); } }; ws.onmessage = function(event) { From d97587b40091e592569bca5f7e5f2f510dcbfad6 Mon Sep 17 00:00:00 2001 From: JamesThorpe Date: Tue, 16 Dec 2014 20:54:58 +0000 Subject: [PATCH 2/4] Updated default, added documentation Now defaults to 30,000. Removed the -1 magic number. Added documentation on use. --- README.md | 5 +++++ reconnecting-websocket.js | 11 ++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 89b72f1..56039b4 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,11 @@ or - Accepts `integer` - Default: `1000` +#### `maxReconnectInterval` +- The maximum number of milliseconds to delay a reconnection attempt. +- Accepts `integer` +- Default: `30000` + ####`reconnectDecay` - The rate of increase of the reconnect delay. Allows reconnect attempts to back off when problems persist. - Accepts `integer` or `float` diff --git a/reconnecting-websocket.js b/reconnecting-websocket.js index 933f049..8a8fd94 100644 --- a/reconnecting-websocket.js +++ b/reconnecting-websocket.js @@ -79,6 +79,9 @@ * reconnectInterval * - The number of milliseconds to delay before attempting to reconnect. Accepts integer. Default: 1000. * + * maxReconnectInterval + * - The maximum number of milliseconds to delay a reconnection attempt. Accepts integer. Default: 30000. + * * reconnectDecay * - The rate of increase of the reconnect delay. Allows reconnect attempts to back off when problems persist. Accepts integer or float. Default: 1.5. * @@ -105,7 +108,7 @@ /** The number of milliseconds to delay before attempting to reconnect. */ reconnectInterval: 1000, /** The maximum number of milliseconds to delay a reconnection attempt. */ - maxReconnectInterval: -1, + maxReconnectInterval: 30000, /** The rate of increase of the reconnect delay. Allows reconnect attempts to back off when problems persist. */ reconnectDecay: 1.5, /** The maximum time in milliseconds to wait for a connection to succeed before closing and retrying. */ @@ -234,14 +237,12 @@ } eventTarget.dispatchEvent(generateEvent('close')); } - var timeout = self.reconnectInterval * Math.pow(self.reconnectDecay, self.reconnectAttempts); - if (self.maxReconnectInterval != -1 && timeout > self.maxReconnectInterval) - timeout = self.maxReconnectInterval; + var timeout = self.reconnectInterval * Math.pow(self.reconnectDecay, self.reconnectAttempts); setTimeout(function() { self.reconnectAttempts++; connect(true); - }, timeout); + }, timeout > self.maxReconnectInterval ? self.maxReconnectInterval : timeout); } }; ws.onmessage = function(event) { From e4de33b981fd1729a2a75bb7d103158e409b99a6 Mon Sep 17 00:00:00 2001 From: JamesThorpe Date: Tue, 16 Dec 2014 21:00:49 +0000 Subject: [PATCH 3/4] Added maxreconnectinterval to ts definition --- reconnecting-websockets.d.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/reconnecting-websockets.d.ts b/reconnecting-websockets.d.ts index 86af5c7..28b4081 100644 --- a/reconnecting-websockets.d.ts +++ b/reconnecting-websockets.d.ts @@ -23,6 +23,9 @@ declare class ReconnectingWebSocket /** The number of milliseconds to delay before attempting to reconnect. */ public reconnectInterval: number; + /** The maximum number of milliseconds to delay a reconnection attempt. */ + public maxReconnectInterval: number; + /** The rate of increase of the reconnect delay. Allows reconnect attempts to back off when problems persist. */ public reconnectDecay: number; From 46399e63f4250bdf16e12f5dc4097250a40eefa9 Mon Sep 17 00:00:00 2001 From: JamesThorpe Date: Tue, 16 Dec 2014 22:16:48 +0000 Subject: [PATCH 4/4] swapped tab for spaces --- reconnecting-websockets.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/reconnecting-websockets.d.ts b/reconnecting-websockets.d.ts index 28b4081..0acf1a3 100644 --- a/reconnecting-websockets.d.ts +++ b/reconnecting-websockets.d.ts @@ -23,8 +23,8 @@ declare class ReconnectingWebSocket /** The number of milliseconds to delay before attempting to reconnect. */ public reconnectInterval: number; - /** The maximum number of milliseconds to delay a reconnection attempt. */ - public maxReconnectInterval: number; + /** The maximum number of milliseconds to delay a reconnection attempt. */ + public maxReconnectInterval: number; /** The rate of increase of the reconnect delay. Allows reconnect attempts to back off when problems persist. */ public reconnectDecay: number;