feat: 修复通知初始化问题

This commit is contained in:
zdl
2025-11-26 10:55:38 +08:00
parent 1090a2fc67
commit 2fcc341213
2 changed files with 38 additions and 17 deletions

View File

@@ -329,10 +329,18 @@ class SocketService {
onSubscribed,
} = options;
if (!this.socket || !this.connected) {
logger.warn('socketService', 'Cannot subscribe: socket not connected');
// 自动连接
this.connect();
// ⚡ 改进状态检查:同时检查 this.connected 和 socket.connected
// 解决 connect 回调中 this.connected 尚未更新的竞争条件
const isReady = this.socket && (this.socket.connected || this.connected);
if (!isReady) {
logger.debug('socketService', 'Socket 尚未就绪,等待连接后订阅');
if (!this.socket) {
// 自动连接
this.connect();
}
// 等待连接成功后再订阅
this.socket.once('connect', () => {
this._doSubscribe(eventType, importance, onNewEvent, onSubscribed);