From ad1da60f65a087867def4bc2f030591f3d789ae6 Mon Sep 17 00:00:00 2001 From: GranceWang <1163260785@qq.com> Date: Wed, 31 Jul 2024 15:49:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20web=E7=AB=AF=E5=92=8Cunity=E7=AB=AF?= =?UTF-8?q?=E7=9A=84=E8=BF=9C=E7=A8=8B=E8=BF=9E=E6=8E=A5store?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/stores/remote-ws-store.ts | 71 +++++++++++++++++------------------ 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/src/stores/remote-ws-store.ts b/src/stores/remote-ws-store.ts index f1ec73d..6f365b7 100644 --- a/src/stores/remote-ws-store.ts +++ b/src/stores/remote-ws-store.ts @@ -1,6 +1,5 @@ -import {defineStore} from "pinia"; -import {Session} from "@/utils/storage"; -import {ElMessage} from "element-plus"; +import { Session } from "@/utils/storage"; +import { defineStore } from "pinia"; const vitalUrl = "ws://" + window.location.host + "/socket.io/admin/rax/vitalSignsMedicine?token=" + Session.getToken() const medicineUrl = "ws://" + window.location.host + "/socket.io/admin/rax/addMedicine?token=" + Session.getToken() @@ -119,7 +118,7 @@ export const useRemoteWsStore = defineStore("remoteWs", { patientName: name, idNum: id, date: date, - msgType: "msg" + msgType: "init" })) } } @@ -294,37 +293,24 @@ export const useRemoteWsStore = defineStore("remoteWs", { }, // 请求连接远程控制 - reqMedicineConnect(name: string, id: string, date: string, index: number, msg: string, flag: string): Promise { - if (!this.patient[name + id + date + index]) this.patient[name + id + date + index] = {} - const patient = this.patient[name + id + date + index] - if (!patient['medicineWS']) { - patient['medicineWS'] = new WebSocket(medicineUrl) - patient.medicineWS.onopen = function () { - patient.medicineWS.send(JSON.stringify({ - patientName: name, - idNum: id, - date: date, - msgType: msg, - webConnectionFlag: flag - })) - } + reqMedicineConnect(params:{name: string, id: string, date: string, index: number, msgType: string, flag: string},cb:any){ + const {name, id, date, index,msgType,flag} = params; + const patient = this.patient[name + id + date+index] + const sendData={ + patientName: name, + idNum: id, + date: date, + msgType: msgType, + webConnectionFlag: flag + } + if (patient) { + patient.medicineWS.send(JSON.stringify(sendData)) + } + if (flag==='0') { + setTimeout(() => { + cb(flag) + }, 100); } - - return new Promise((resolve) => { - patient.medicineWS.onmessage = (e: any) => { - const msg = JSON.parse(e.data) - if (msg && msg.msgType === 'unityResponseConnection') { - if (msg.unityConnectionFlag === '1') { - // unity回应 连接成功 - resolve(1); - } - if (msg.unityConnectionFlag === '0') { - // unity回应连接失败, or unity请求断连 - resolve(0); - } - } - } - }); }, subscribeMedicine(name: string, id: string, date: string, index: number, cb: any) { @@ -333,11 +319,17 @@ export const useRemoteWsStore = defineStore("remoteWs", { patient.medicineWS.onmessage = (e: any) => { if (e && e.data) { const data = JSON.parse(e.data); - if (data.msgType == "msg") { - cb(e) + + if (data.msgType==='msg') { + cb(e) } else { patient.medicineWS.send(JSON.stringify({msgType: "heartbeat"})) } + // 抛出信息用于vue页面监听 + const shouldCallBackList=['pong','unityRequestConnection','unityResponseConnection'] + if (shouldCallBackList.includes(data.msgType)) { + cb(e) + } } } } else { @@ -347,6 +339,12 @@ export const useRemoteWsStore = defineStore("remoteWs", { }) } }, + loopSendMedicinePing(name: string, id: string, date: string, index: number, cb: any){ + const patient = this.patient[name + id + date + index] + if (patient) { + patient.medicineWS.send(JSON.stringify({msgType: "ping"})) + } + }, medicineOnclose(name: string, id: string, date: string, index: number, cb: any) { const patient = this.patient[name + id + date + index] patient.medicineWS.onclose = () => { @@ -385,6 +383,7 @@ export const useRemoteWsStore = defineStore("remoteWs", { medicine: string, value: string }, cb: any) { + const patient: any = this.patient[args.name + args.id + args.date + args.index] if (patient) { const params = {