diff --git a/webapp/src/wsclient.ts b/webapp/src/wsclient.ts index e88ab2043..893e68d55 100644 --- a/webapp/src/wsclient.ts +++ b/webapp/src/wsclient.ts @@ -127,6 +127,8 @@ class WSClient { onUnfollowBlock: FollowChangeHandler = () => {} private notificationDelay = 100 private reopenDelay = 3000 + private reopenRetryCount = 0 + private reopenMaxRetries = 10 private updatedData: UpdatedData = {Blocks: [], Categories: [], BoardCategories: [], Boards: [], BoardMembers: [], CategoryOrder: []} private updateTimeout?: NodeJS.Timeout private errorPollId?: NodeJS.Timeout @@ -400,6 +402,7 @@ class WSClient { ws.onopen = () => { Utils.log('WSClient webSocket opened.') this.state = 'open' + this.reopenRetryCount = 0 // if has a token defined when connecting, authenticate if (this.token) { @@ -426,19 +429,25 @@ class WSClient { Utils.log(`WSClient websocket onclose, code: ${e.code}, reason: ${e.reason}`) if (ws === this.ws) { // Unexpected close, re-open - Utils.logError('Unexpected close, re-opening websocket') + Utils.logError('Unexpected WSClient close') for (const handler of this.onStateChange) { handler(this, 'close') } this.state = 'close' - setTimeout(() => { - // ToDo: assert that this actually runs the onopen - // contents (auth + this.subscribe()) - this.open() - for (const handler of this.onReconnect) { - handler(this) - } - }, this.reopenDelay) + + if (this.reopenRetryCount < this.reopenMaxRetries) { + setTimeout(() => { + this.reopenRetryCount++ + Utils.log(`Reopening websocket connection, count: ${this.reopenRetryCount}`) + + this.open() + for (const handler of this.onReconnect) { + handler(this) + } + }, this.reopenDelay) + } else { + Utils.logError('Reached max websocket re-opening attempts') + } } }