feat: web端和unity端的远程连接store

This commit is contained in:
GranceWang 2024-07-31 15:49:13 +08:00
parent ed81f95e9d
commit ad1da60f65

View File

@ -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<number> {
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 = {