websocket re-subscribe on connection restore

This commit is contained in:
Yuri Kuznetsov
2024-02-09 12:21:58 +02:00
parent bde2e0284a
commit 7f75ee6ed8
+22 -1
View File
@@ -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();
}