From e6465d06462e492eb170503d3454fdcdf2380499 Mon Sep 17 00:00:00 2001 From: Paul Makles Date: Thu, 31 Dec 2020 13:04:49 +0000 Subject: [PATCH] Exponential backoff on reconnect. --- package.json | 1 + src/Client.ts | 1 + src/tester.ts | 12 ++++++++++++ src/websocket/client.ts | 8 +++++++- yarn.lock | 5 +++++ 5 files changed, 26 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 99cfb6e1..8671832a 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "dependencies": { "axios": "^0.19.2", "concurrently": "^5.1.0", + "exponential-backoff": "^3.1.0", "isomorphic-ws": "^4.0.1", "lodash": "^4.17.15", "tsc-watch": "^4.1.0", diff --git a/src/Client.ts b/src/Client.ts index e9c18c83..eb97b20f 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -21,6 +21,7 @@ export interface ClientOptions { export declare interface Client { // WebSocket related events. on(event: 'connected', listener: () => void): this; + on(event: 'connecting', listener: () => void): this; on(event: 'dropped', listener: () => void): this; on(event: 'ready', listener: () => void): this; } diff --git a/src/tester.ts b/src/tester.ts index 74fb5003..5fc8451b 100644 --- a/src/tester.ts +++ b/src/tester.ts @@ -8,6 +8,18 @@ client.on('ready', () => { console.log(`Logged in as @${client.user?.username}`); }); +client.on('connecting', () => { + console.log(`Connecting to the notifications server.`); +}); + +client.on('connected', () => { + console.log(`Connected to notifications server.`); +}); + +client.on('dropped', () => { + console.log(`Connection dropped.`); +}); + (async () => { console.log('Start:', new Date()); diff --git a/src/websocket/client.ts b/src/websocket/client.ts index ef6bc767..80059e57 100644 --- a/src/websocket/client.ts +++ b/src/websocket/client.ts @@ -1,4 +1,5 @@ import WebSocket from 'isomorphic-ws'; +import { backOff } from 'exponential-backoff'; import { Client } from '..'; import { Auth } from '../api/auth'; @@ -14,7 +15,9 @@ export class WebSocketClient { this.client = client; } - connect(): Promise { + connect(disallowReconnect?: boolean): Promise { + this.client.emit('connecting'); + return new Promise((resolve, $reject) => { let thrown = false; const reject = (err: any) => { @@ -63,6 +66,9 @@ export class WebSocketClient { ws.onclose = () => { this.client.emit('dropped'); + if (!disallowReconnect && this.client.options.autoReconnect) { + backOff(() => this.connect(true)).catch(reject); + } }; }); } diff --git a/yarn.lock b/yarn.lock index 8aa849a1..b1c7dde9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -164,6 +164,11 @@ event-stream@=3.3.4: stream-combiner "~0.0.4" through "~2.3.1" +exponential-backoff@^3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/exponential-backoff/-/exponential-backoff-3.1.0.tgz#9409c7e579131f8bd4b32d7d8094a911040f2e68" + integrity sha512-oBuz5SYz5zzyuHINoe9ooePwSu0xApKWgeNzok4hZ5YKXFh9zrQBEM15CXqoZkJJPuI2ArvqjPQd8UKJA753XA== + find-up@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/find-up/-/find-up-3.0.0.tgz#49169f1d7993430646da61ecc5ae355c21c97b73"