From fabfa05db5ad4e45e6d8dd28da668920b5637c82 Mon Sep 17 00:00:00 2001 From: lixiaolong <702156524@qq.com> Date: Fri, 14 Aug 2026 13:31:37 +0800 Subject: [PATCH] =?UTF-8?q?fix(websocket):=20=E8=A7=A3=E5=86=B3WebSocket?= =?UTF-8?q?=E9=87=8D=E8=BF=9E=E5=90=8E=E9=A2=91=E9=81=93=E8=AE=A2=E9=98=85?= =?UTF-8?q?=E4=B8=A2=E5=A4=B1=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除调试用的console.log语句 - 添加channelSubscriptions集合跟踪频道订阅状态 - 在WebSocket连接建立时恢复之前的频道订阅 - 添加refreshAfterSocketReconnect函数用于重连后刷新数据 - 在组件挂载时监听socket open事件并绑定重连刷新逻辑 - 在组件卸载时移除socket open事件监听器 --- src/plugins/websocket.js | 22 ++++++++++++++++++- src/views/inspection/runTask/index.vue | 7 +++++- .../intelligenceTest/runningTask/index.vue | 8 +++++-- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/plugins/websocket.js b/src/plugins/websocket.js index f3ca635..2f2ee77 100644 --- a/src/plugins/websocket.js +++ b/src/plugins/websocket.js @@ -17,6 +17,7 @@ class WebSocketManager { this.processingMessages = false; this.maxQueueSize = 100; this.status = false; + this.channelSubscriptions = new Set(); } connect() { @@ -33,6 +34,7 @@ class WebSocketManager { this.status = true; this.startHeartbeat(); this.processMessageQueue(); + this.restoreChannelSubscriptions(); this.dispatch('open'); }; @@ -159,13 +161,31 @@ class WebSocketManager { } send(payload) { + if (payload?.type === 'channel_subscription' && payload.channel) { + if (payload.action === 'subscribe') { + this.channelSubscriptions.add(payload.channel); + } else if (payload.action === 'unsubscribe') { + this.channelSubscriptions.delete(payload.channel); + } + } if (this.ws && this.ws.readyState === 1) { this.ws.send(JSON.stringify(payload)); - } else { + } else if (payload?.type !== 'channel_subscription') { console.warn('WebSocket 未连接,消息发送失败'); } } + restoreChannelSubscriptions() { + if (!this.ws || this.ws.readyState !== 1) return; + this.channelSubscriptions.forEach((channel) => { + this.ws.send(JSON.stringify({ + type: 'channel_subscription', + action: 'subscribe', + channel, + })); + }); + } + close() { this.isManualClose = true; this.status = false; diff --git a/src/views/inspection/runTask/index.vue b/src/views/inspection/runTask/index.vue index f121dc6..8e1b67f 100644 --- a/src/views/inspection/runTask/index.vue +++ b/src/views/inspection/runTask/index.vue @@ -501,7 +501,6 @@ const subscribeDebounced = debounce(async (sub) => { // 清理旧回调 if (socketChannelCallbacks.value[channel]) { socket.off(channel, socketChannelCallbacks.value[channel]); - console.log('清理旧回调:', channel); } const callback = (data) => { @@ -555,15 +554,21 @@ const handleOpenLog = async (row) => { logDialogData.value = res.rows } } +const refreshAfterSocketReconnect = () => { + getList() + getHistoryList() +} onMounted(() => { getTaskData(); getList(); getHistoryList(); subscribeDebounced(true) + socket?.on('open', refreshAfterSocketReconnect) }) onUnmounted(() => { subscribeDebounced(false) + socket?.off('open', refreshAfterSocketReconnect) })