Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
9 changes: 8 additions & 1 deletion reconnecting-websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -104,6 +107,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: 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. */
Expand Down Expand Up @@ -232,10 +237,12 @@
}
eventTarget.dispatchEvent(generateEvent('close'));
}

var timeout = self.reconnectInterval * Math.pow(self.reconnectDecay, self.reconnectAttempts);
setTimeout(function() {
self.reconnectAttempts++;
connect(true);
}, self.reconnectInterval * Math.pow(self.reconnectDecay, self.reconnectAttempts));
}, timeout > self.maxReconnectInterval ? self.maxReconnectInterval : timeout);
Copy link
Owner

Choose a reason for hiding this comment

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

Simplify: Max.min(timeout, self.maxReconnectInterval)

}
};
ws.onmessage = function(event) {
Expand Down
3 changes: 3 additions & 0 deletions reconnecting-websockets.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Copy link
Owner

Choose a reason for hiding this comment

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

Indentation does not match rest of file

public maxReconnectInterval: number;

/** The rate of increase of the reconnect delay. Allows reconnect attempts to back off when problems persist. */
public reconnectDecay: number;

Expand Down