From a8889e1ce62e25e8bd33227080b2966fe7438edd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=A1=E5=A4=8D=E4=B9=A0?= <2353956224@qq.com> Date: Fri, 22 Aug 2025 16:59:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E5=8C=85=20=E8=A7=A6=E5=8F=91?= =?UTF-8?q?=E5=B1=8F=E5=B9=95=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/composables/useDeviceDiscovery.js | 102 ++++++++++++ src/composables/useTouch.js | 109 +++++++++++++ src/views/VideoStream.vue | 222 +++++--------------------- 3 files changed, 247 insertions(+), 186 deletions(-) create mode 100644 src/composables/useDeviceDiscovery.js create mode 100644 src/composables/useTouch.js diff --git a/src/composables/useDeviceDiscovery.js b/src/composables/useDeviceDiscovery.js new file mode 100644 index 0000000..c46c478 --- /dev/null +++ b/src/composables/useDeviceDiscovery.js @@ -0,0 +1,102 @@ +// src/composables/useDeviceDiscovery.js +import { nextTick } from 'vue' + +/** + * 负责连接 multiplex WebSocket,维护 deviceInformation 列表, + * 并在设备出现时按顺序调用 initCanvas / initVideoStream / getSize。 + * 获取设备的ws + */ +export function useDeviceDiscovery({ + deviceInformation, // ref([]) + initCanvas, // (udid) => void + initVideoStream, // (udid, index) => void + wsActionsRef, // () => wsActions (可能一开始是 null) + td // 你的 useTeardown 实例,可选 +}) { + const decoder = new TextDecoder('utf-8') + + function open(url, eitwoBuffer) { + const ws = new WebSocket(url) + td && td.setMultiplexWS && td.setMultiplexWS(ws) // 交给 teardown 托管 + ws.binaryType = 'arraybuffer' + + ws.onopen = () => { + ws.send(eitwoBuffer) // 请求设备列表 + } + + ws.onmessage = async (event) => { + let data + try { + // 兼容原逻辑:先解码再清理不可见字符再 JSON 解析 + data = JSON.parse(decoder.decode(event.data).replace(/[^\x20-\x7F]/g, '')) + } catch (e) { + console.error('[multiplex] parse error:', e) + return + } + + if (data.type === 'devicelist') { + // 全量刷新 + deviceInformation.value = [] + const list = (data.data.list || []).filter(it => it.state === 'device') + + for (let i = 0; i < list.length; i++) { + const item = list[i] + deviceInformation.value.push(item) + + await nextTick() // 等 v-for 渲染出