From e3c57670247d926fea954c59cdd6cc5b1215d922 Mon Sep 17 00:00:00 2001 From: wdvipa Date: Sun, 15 Feb 2026 17:33:56 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=AE=BE=E5=A4=87=E7=A6=BB=E7=BA=BF?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E5=90=8C=E6=AD=A5=E9=97=AE=E9=A2=98=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - device_disconnected改为设置offline状态而非移除设备 - device_disconnected兼容字符串和对象两种数据格式 - 新增devices_list_refresh事件监听,每10秒全量同步设备列表 - 解决设备在线但Web端显示离线的根本问题 --- src/components/RemoteControlApp.tsx | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/components/RemoteControlApp.tsx b/src/components/RemoteControlApp.tsx index d6ce18b..6496f33 100644 --- a/src/components/RemoteControlApp.tsx +++ b/src/components/RemoteControlApp.tsx @@ -290,6 +290,19 @@ const RemoteControlApp: React.FC = () => { } }) + // 定期设备列表全量刷新(服务端每10秒推送) + socket.on('devices_list_refresh', (data) => { + if (!data?.devices || !Array.isArray(data.devices)) { + return + } + data.devices.forEach((device: any) => { + if (!device?.id) { + return + } + dispatch(addDevice(device)) + }) + }) + socket.on('device_connected', (device) => { console.log('设备已连接:', device) dispatch(addDevice(device)) @@ -306,9 +319,19 @@ const RemoteControlApp: React.FC = () => { })) }) - socket.on('device_disconnected', (deviceId) => { - console.log('设备已断开:', deviceId) - dispatch(removeDevice(deviceId)) + socket.on('device_disconnected', (data) => { + // 兼容两种格式: 字符串deviceId 或 对象{deviceId} + const deviceId = typeof data === 'string' ? data : data?.deviceId + if (!deviceId) { + console.error('[Device] device_disconnected: invalid data', data) + return + } + console.log('[Device] disconnected:', deviceId) + // 将设备状态设为offline而不是移除,避免设备重连时丢失 + dispatch(updateDeviceConnectionStatus({ + deviceId, + status: 'offline' + })) dispatch(addNotification({ type: 'info', title: '设备已断开',