From 06ec029555b21df83e08f2a8bb6add23bbf350ac Mon Sep 17 00:00:00 2001 From: zoujing Date: Thu, 8 Jan 2026 17:01:47 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E9=87=8D=E8=BF=9E=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/chat/ChatMainList/index.vue | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/pages/index/components/chat/ChatMainList/index.vue b/src/pages/index/components/chat/ChatMainList/index.vue index 852e2e5..f15a943 100644 --- a/src/pages/index/components/chat/ChatMainList/index.vue +++ b/src/pages/index/components/chat/ChatMainList/index.vue @@ -662,13 +662,31 @@ const sendWebSocketMessage = async (messageType, messageContent, options = {}) = try { const raw = webSocketManager.sendMessage(args); // 兼容可能返回同步布尔或 Promise 的实现 - const result = await Promise.resolve(raw); - if (result) { - console.log(`WebSocket消息已发送 [类型:${messageType}]:`, args); - return true; - } - // 若返回 false,准备重试 - console.warn('webSocketManager.sendMessage 返回 false,准备重试', { attempt, args }); + const result = await Promise.resolve(raw); + if (result) { + console.log(`WebSocket消息已发送 [类型:${messageType}]:`, args); + return true; + } + + // 若返回 false,消息可能已经被 manager 入队并触发连接流程。 + // 在这种情况下避免立即当作失败处理,而是等待短暂时间以观察连接是否建立并由 manager 发送队列。 + console.warn('webSocketManager.sendMessage 返回 false,等待连接或队列发送...', { attempt, args }); + const waitForConnectMs = typeof options.waitForConnectMs === 'number' ? options.waitForConnectMs : 5000; + if (webSocketManager && typeof webSocketManager.isConnected === 'function' && !webSocketManager.isConnected()) { + const startTs = Date.now(); + while (Date.now() - startTs < waitForConnectMs) { + await sleep(200); + if (webSocketManager.isConnected()) { + // 给 manager 一点时间处理队列并发送 + await sleep(150); + console.log('检测到 manager 已连接,假定队列消息已发送', args); + return true; + } + } + console.warn('等待 manager 建连超时,进入重试逻辑', { waitForConnectMs, args }); + } else { + console.warn('sendMessage 返回 false 但 manager 看起来已连接或不可用,继续重试', { args }); + } } catch (error) { console.error('发送WebSocket消息异常:', error, args); }