Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a maximum number of retries to reconnect #59

Closed
wants to merge 1 commit into from
Closed
Changes from all 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
22 changes: 19 additions & 3 deletions src/y-websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const setupWS = provider => {
websocket.send(encoding.toUint8Array(encoder))
}
}
websocket.onclose = () => {
websocket.onclose = event => {
provider.ws = null
provider.wsconnecting = false
if (provider.wsconnected) {
Expand All @@ -116,6 +116,16 @@ const setupWS = provider => {
} else {
provider.wsUnsuccessfulReconnects++
}


if (provider._maxReconnectRetryCount > 0 && provider.wsUnsuccessfulReconnects > provider._maxReconnectRetryCount) {
provider.shouldConnect = false;
provider.wsUnsuccessfulReconnects = 0;
provider.emit('status', [{
status: 'connection-error',
wsEvent: event
}])
}
// Start with no reconnect timeout and increase timeout by
// log10(wsUnsuccessfulReconnects).
// The idea is to increase reconnect timeout slowly and have no reconnect
Expand Down Expand Up @@ -187,10 +197,11 @@ export class WebsocketProvider extends Observable {
* @param {boolean} [opts.connect]
* @param {awarenessProtocol.Awareness} [opts.awareness]
* @param {Object<string,string>} [opts.params]
* @param {typeof WebSocket} [opts.WebSocketPolyfill] Optionall provide a WebSocket polyfill
* @param {typeof WebSocket} [opts.WebSocketPolyfill] Optional provide a WebSocket polyfill
* @param {number} [opts.resyncInterval] Request server state every `resyncInterval` milliseconds
* @param {number} [opts.maxReconnectRetryCount] The maximum number of retries to reconnect.
*/
constructor (serverUrl, roomname, doc, { connect = true, awareness = new awarenessProtocol.Awareness(doc), params = {}, WebSocketPolyfill = WebSocket, resyncInterval = -1 } = {}) {
constructor (serverUrl, roomname, doc, { connect = true, awareness = new awarenessProtocol.Awareness(doc), params = {}, WebSocketPolyfill = WebSocket, resyncInterval = -1, maxReconnectRetryCount = -1 } = {}) {
super()
// ensure that url is always ends with /
while (serverUrl[serverUrl.length - 1] === '/') {
Expand Down Expand Up @@ -240,6 +251,11 @@ export class WebsocketProvider extends Observable {
}, resyncInterval))
}

/**
* @type {number}
*/
this._maxReconnectRetryCount = maxReconnectRetryCount;

/**
* @param {ArrayBuffer} data
*/
Expand Down