From c6e8b600398de38b6684f5fa1eaaaade8562859b Mon Sep 17 00:00:00 2001 From: wangxinhui <wangxinhui@hnkhzn.com> Date: 星期六, 20 九月 2025 15:16:56 +0800 Subject: [PATCH] Merge branch 'master' of http://115.159.85.185:8098/r/ZhiHuiQiCe/LongDeLiLiKu --- 项目代码/WCS/WCSClient/src/uitils/websocket.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 56 insertions(+), 0 deletions(-) diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WCSClient/src/uitils/websocket.js" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WCSClient/src/uitils/websocket.js" new file mode 100644 index 0000000..d15ff7f --- /dev/null +++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WCSClient/src/uitils/websocket.js" @@ -0,0 +1,56 @@ +import { ref, reactive } from 'vue'; + +export const useWebSocket = (protocols) => { + const ws = new WebSocket("ws://localhost:9260/", protocols); + const messages = ref([]); + const status = reactive({ + isConnected: false, + error: null + }); + + // 浜嬩欢鐩戝惉 + const connect = () => { + ws.onopen = () => { + status.isConnected = true; + console.log('WebSocket 杩炴帴鎴愬姛'); + }; + + ws.onmessage = (event) => { + try { + const data = JSON.parse(event.data); + messages.value.push(data); + } catch (e) { + console.error('娑堟伅瑙f瀽澶辫触', e); + } + }; + + ws.onclose = (event) => { + status.isConnected = false; + console.log('杩炴帴鍏抽棴锛屽噯澶囬噸杩�...'); + setTimeout(connect, 3000); // 3绉掑悗閲嶈繛 + }; + + ws.onerror = (error) => { + status.error = error; + console.error('WebSocket 閿欒:', error); + }; + }; + + // 鍒濆鍖栬繛鎺� + connect(); + + // 鍙戦�佹秷鎭柟娉� + const send = (data) => { + if (status.isConnected) { + ws.send(JSON.stringify(data)); + } + }; + + return { + ws, + messages, + status, + send, + close: () => ws.close() + }; +}; \ No newline at end of file -- Gitblit v1.9.3