websocket reconnect subscribe

This commit is contained in:
Yuri Kuznetsov
2025-05-07 09:17:42 +03:00
parent e394779737
commit 7359367569
2 changed files with 51 additions and 3 deletions
+7 -1
View File
@@ -304,7 +304,13 @@ class NotificationBadgeView extends View {
}, this.waitInterval * 1000);
};
this.getHelper().webSocketManager.subscribe('newNotification', () => onWebSocketNewNotification());
const webSocketManager = this.getHelper().webSocketManager;
webSocketManager.subscribe('newNotification', () => onWebSocketNewNotification());
webSocketManager.subscribeToReconnect(onWebSocketNewNotification);
this.once('remove', () => webSocketManager.unsubscribe('newNotification'));
this.once('remove', () => webSocketManager.unsubscribeFromReconnect(onWebSocketNewNotification));
}
/**
+44 -2
View File
@@ -62,6 +62,12 @@ class WebSocketManager {
*/
this.config = config;
/**
* @private
* @type {Function[]}
*/
this.subscribeToReconnectQueue = [];
/**
* @private
* @type {{category: string, callback: Function}[]}
@@ -171,6 +177,8 @@ class WebSocketManager {
* @param {string} url
*/
connectInternal(auth, userId, url) {
let wasConnected = false;
this.connection = new ab.Session(
url,
() => {
@@ -182,7 +190,13 @@ class WebSocketManager {
this.subscribeQueue = [];
if (wasConnected) {
this.subscribeToReconnectQueue.forEach(callback => callback());
}
this.schedulePing();
wasConnected = true;
},
code => {
if (code === ab.CONNECTION_CLOSED) {
@@ -204,6 +218,26 @@ class WebSocketManager {
);
}
/**
* Subscribe to reconnecting.
*
* @param {function(): void} callback A callback.
* @since 9.1.1
*/
subscribeToReconnect(callback) {
this.subscribeToReconnectQueue.push(callback);
}
/**
* Unsubscribe from reconnecting.
*
* @param {function(): void} callback A callback.
* @since 9.1.1
*/
unsubscribeFromReconnect(callback) {
this.subscribeToReconnectQueue = this.subscribeToReconnectQueue.filter(it => it !== callback);
}
/**
* Subscribe to a topic.
*
@@ -254,11 +288,19 @@ class WebSocketManager {
}
this.subscribeQueue = this.subscribeQueue.filter(item => {
return item.category !== category && item.callback !== callback;
if (callback === undefined) {
return item.category !== category;
}
return item.category !== category || item.callback !== callback;
});
this.subscriptions = this.subscriptions.filter(item => {
return item.category !== category && item.callback !== callback;
if (callback === undefined) {
return item.category !== category;
}
return item.category !== category || item.callback !== callback;
});
try {