Skip to content

Commit

Permalink
Added option for maxReconnectInterval
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JamesThorpe committed Dec 16, 2014
1 parent ee8f794 commit 39e1438
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion reconnecting-websocket.js
Expand Up @@ -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. */
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 39e1438

Please sign in to comment.