From 7f75ee6ed8d3433989c33f2b9490fa9f86996ef2 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Fri, 9 Feb 2024 12:21:58 +0200 Subject: [PATCH] websocket re-subscribe on connection restore --- client/src/web-socket-manager.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/client/src/web-socket-manager.js b/client/src/web-socket-manager.js index bc46d2d44b..2f20d9aa65 100644 --- a/client/src/web-socket-manager.js +++ b/client/src/web-socket-manager.js @@ -51,6 +51,12 @@ class WebSocketManager { */ this.subscribeQueue = []; + /** + * @private + * @type {{category: string, callback: Function}[]} + */ + this.subscribtions = []; + /** * @private * @type {boolean} @@ -152,6 +158,9 @@ class WebSocketManager { } if (e === ab.CONNECTION_LOST || e === ab.CONNECTION_UNREACHABLE) { + this.subscribeQueue = this.subscribtions; + this.subscribtions = []; + setTimeout(() => this.connect(auth, userId), 3000); } }, @@ -169,7 +178,7 @@ class WebSocketManager { * Subscribe to a topic. * * @param {string} category A topic. - * @param {Function} callback A callback. + * @param {function(string, *): void} callback A callback. */ subscribe(category, callback) { if (!this.connection) { @@ -187,6 +196,11 @@ class WebSocketManager { try { this.connection.subscribe(category, callback); + + this.subscribtions.push({ + category: category, + callback: callback, + }); } catch (e) { if (e.message) { @@ -213,6 +227,10 @@ class WebSocketManager { return item.category !== category && item.callback !== callback; }); + this.subscribtions = this.subscribtions.filter(item => { + return item.category !== category && item.callback !== callback; + }); + try { this.connection.unsubscribe(category, callback); } @@ -234,6 +252,9 @@ class WebSocketManager { return; } + this.subscribeQueue = []; + this.subscribtions = []; + try { this.connection.close(); }