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) })