From ddc2b287262736ce6eb74777fb4f1e47a5323771 Mon Sep 17 00:00:00 2001 From: republicline <1464474399@qq.com> Date: Sun, 7 Sep 2025 22:36:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D,=E5=A4=9A=E4=B8=AA=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5,=E7=8A=B6=E6=80=81=E6=8F=90=E7=A4=BA=E9=94=99?= =?UTF-8?q?=E8=AF=AF,=20=E5=B9=B6=E4=B8=94=E5=8E=BB=E9=99=A4=E7=B4=A2?= =?UTF-8?q?=E5=BC=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/stores/remote-ws-store.ts | 210 +++++++++++++++--- .../remote-manage/part/remote-item-part.vue | 3 +- src/views/remote-manage/part/remote-part.vue | 3 +- 3 files changed, 182 insertions(+), 34 deletions(-) diff --git a/src/stores/remote-ws-store.ts b/src/stores/remote-ws-store.ts index 3ac0075..4234ef3 100644 --- a/src/stores/remote-ws-store.ts +++ b/src/stores/remote-ws-store.ts @@ -67,8 +67,14 @@ export const useRemoteWsStore = defineStore("remoteWs", { } }, setCurrentTaskIndex(i: number) { - this.currentTaskIndex = i - Session.set("currentTaskIndex", i) + // this.currentTaskIndex = i + // Session.set("currentTaskIndex", i) + // 切换前清除旧任务的重连定时器 + if (this.currentTaskIndex !== -1) { + this.clearReconnectTimer(this.currentTaskIndex); + } + this.currentTaskIndex = i; + Session.set("currentTaskIndex", i); }, getCurrentTaskIndex() { if (Session.get("currentTaskIndex") > -1) { @@ -215,29 +221,88 @@ export const useRemoteWsStore = defineStore("remoteWs", { } },*/ + // disconnect(name: string, id: string, date: string, index: number) { + // this.disconnectChat(name, id, date, index) + // this.disconnectVital(name, id, date, index) + // delete this.patient[name + id + date + index] + // }, disconnect(name: string, id: string, date: string, index: number) { - this.disconnectChat(name, id, date, index) - this.disconnectVital(name, id, date, index) - delete this.patient[name + id + date + index] + this.disconnectChat(name, id, date, index); + this.disconnectVital(name, id, date, index); + const patientKey = `${name}${id}${date}${index}`; + delete this.patient[patientKey]; + this.setActiveDisconnect(index, true); // 确保标记主动断开 + this.clearReconnectTimer(index); // 最终清理定时器 }, + // createVital(name: string, id: string, date: string, index: number) { + // const patient = this.patient[name + id + date + index] ? this.patient[name + id + date + index] : this.patient[name + id + date + index] = {} + // if (patient && !patient['vitalWS']) { + // patient['vitalWS'] = new WebSocket(vitalUrl) + // patient['vitalWS'].onopen = function () { + // ElMessage.info("远程查看" + (index + 1) + ",生命体征数据连接成功") + // patient['vitalWS'].send(JSON.stringify({ + // patientName: name, + // idNum: id, + // date: date, + // msgType: "msg" + // })) + // } + // + // patient['vitalWS'].onerror = () => { + // ElMessage.error("远程管理" + (index + 1) + "生命体征数据出错") + // } + // delete this.closeStatus[name + id + date + index + 'vitalWS'] + // } + // }, createVital(name: string, id: string, date: string, index: number) { - const patient = this.patient[name + id + date + index] ? this.patient[name + id + date + index] : this.patient[name + id + date + index] = {} + const patientKey = `${name}${id}${date}${index}`; + const patient = this.patient[patientKey] ? this.patient[patientKey] : this.patient[patientKey] = {}; + // 新增:若已主动断开,直接返回不创建连接 + if (this.isActiveDisconnect(index)) { + return; + } if (patient && !patient['vitalWS']) { - patient['vitalWS'] = new WebSocket(vitalUrl) - patient['vitalWS'].onopen = function () { - ElMessage.info("远程查看" + (index + 1) + ",生命体征数据连接成功") + patient['vitalWS'] = new WebSocket(vitalUrl); + patient['vitalWS'].onopen = () => { + if (index === this.currentTaskIndex) { + ElMessage.info("远程查看,生命体征数据连接成功"); + } patient['vitalWS'].send(JSON.stringify({ patientName: name, idNum: id, date: date, msgType: "msg" - })) - } + })); + this.clearReconnectTimer(index); + }; patient['vitalWS'].onerror = () => { - ElMessage.error("远程管理" + (index + 1) + "生命体征数据出错") - } - delete this.closeStatus[name + id + date + index + 'vitalWS'] + if (index === this.currentTaskIndex) { + ElMessage.error("远程管理,生命体征数据出错"); + } + }; + + patient['vitalWS'].onclose = () => { + // 新增:若已主动断开,不触发重连 + if (this.isActiveDisconnect(index)) { + delete patient.vitalWS; + return; + } + if (index === this.currentTaskIndex) { + ElMessage.warning("远程查看,生命体征连接断开,正在重连..."); + this.clearReconnectTimer(index); + const timerId = window.setTimeout(() => { + // 重连前再次检查是否已主动断开 + if (!this.isActiveDisconnect(index)) { + this.createVital(name, id, date, index); + } + }, 3000); + this.setReconnectTimer(index, timerId); + } + delete patient.vitalWS; + }; + + delete this.closeStatus[`${patientKey}vitalWS`]; } }, subscribeVital(name: string, id: string, date: string, index: number, cb: any) { @@ -313,38 +378,108 @@ export const useRemoteWsStore = defineStore("remoteWs", { } }, + // disconnectVital(name: string, id: string, date: string, index: number) { + // const patient: any = this.patient[name + id + date + index] + // if (patient && patient.vitalWS) { + // this.closeStatus[name + id + date + index + 'vitalWS'] = true + // patient.vitalWS.close() + // delete patient['vitalWS'] + // } + // }, disconnectVital(name: string, id: string, date: string, index: number) { - const patient: any = this.patient[name + id + date + index] + const patientKey = `${name}${id}${date}${index}`; + const patient: any = this.patient[patientKey]; if (patient && patient.vitalWS) { - this.closeStatus[name + id + date + index + 'vitalWS'] = true - patient.vitalWS.close() - delete patient['vitalWS'] + this.closeStatus[`${patientKey}vitalWS`] = true; + this.setActiveDisconnect(index, true); // 标记为主动断开 + patient.vitalWS.close(); + delete patient.vitalWS; + this.clearReconnectTimer(index); // 强制清除重连定时器 } }, + + + // createChat(name: string, id: string, date: string, index: number) { + // const patient = this.patient[name + id + date + index] ? this.patient[name + id + date + index] : this.patient[name + id + date + index] = {} + // if (patient && !patient['chatWS']) { + // patient['chatWS'] = new WebSocket(chatUrl) + // patient['chatWS'].onopen = function () { + // ElMessage.info("远程查看" + (index + 1) + ",通讯连接成功") + // patient['chatWS'].send(JSON.stringify({ + // patientName: name, + // idNum: id, + // date: date, + // msgType: "init" + // })) + // patient['chatWS'].send(JSON.stringify({ + // patientName: name, + // idNum: id, + // date: date, + // msgType: "historyChat" + // })) + // } + // + // patient['chatWS'].onerror = () => { + // ElMessage.error("远程查看" + (index + 1) + "通讯出错") + // } + // delete this.closeStatus[name + id + date + index + 'chatWS'] + // } + // }, createChat(name: string, id: string, date: string, index: number) { - const patient = this.patient[name + id + date + index] ? this.patient[name + id + date + index] : this.patient[name + id + date + index] = {} + const patientKey = `${name}${id}${date}${index}`; + const patient = this.patient[patientKey] ? this.patient[patientKey] : this.patient[patientKey] = {}; + // 新增:若已主动断开,直接返回不创建连接 + if (this.isActiveDisconnect(index)) { + return; + } if (patient && !patient['chatWS']) { - patient['chatWS'] = new WebSocket(chatUrl) - patient['chatWS'].onopen = function () { - ElMessage.info("远程查看" + (index + 1) + ",通讯连接成功") + patient['chatWS'] = new WebSocket(chatUrl); + patient['chatWS'].onopen = () => { + if (index === this.currentTaskIndex) { + ElMessage.info("远程查看,通讯连接成功"); + } patient['chatWS'].send(JSON.stringify({ patientName: name, idNum: id, date: date, msgType: "init" - })) + })); patient['chatWS'].send(JSON.stringify({ patientName: name, idNum: id, date: date, msgType: "historyChat" - })) - } + })); + this.clearReconnectTimer(index); + }; patient['chatWS'].onerror = () => { - ElMessage.error("远程查看" + (index + 1) + "通讯出错") - } - delete this.closeStatus[name + id + date + index + 'chatWS'] + if (index === this.currentTaskIndex) { + ElMessage.error("远程查看,通讯出错"); + } + }; + + patient['chatWS'].onclose = () => { + // 新增:若已主动断开,不触发重连 + if (this.isActiveDisconnect(index)) { + delete patient.chatWS; + return; + } + if (index === this.currentTaskIndex) { + ElMessage.warning("远程查看,通讯连接断开,正在重连..."); + this.clearReconnectTimer(index); + const timerId = window.setTimeout(() => { + // 重连前再次检查是否已主动断开 + if (!this.isActiveDisconnect(index)) { + this.createChat(name, id, date, index); + } + }, 3000); + this.setReconnectTimer(index, timerId); + } + delete patient.chatWS; + }; + + delete this.closeStatus[`${patientKey}chatWS`]; } }, subscribeChat(name: string, id: string, date: string, index: number, cb: any) { @@ -383,12 +518,23 @@ export const useRemoteWsStore = defineStore("remoteWs", { const patient: any = this.patient[name + id + date + index] patient.chatWS.onmessage = undefined; }, + // disconnectChat(name: string, id: string, date: string, index: number) { + // const patient: any = this.patient[name + id + date + index] + // if (patient && patient.chatWS) { + // this.closeStatus[name + id + date + index + 'chatWS'] = true + // patient.chatWS.close() + // delete patient['chatWS'] + // } + // }, disconnectChat(name: string, id: string, date: string, index: number) { - const patient: any = this.patient[name + id + date + index] + const patientKey = `${name}${id}${date}${index}`; + const patient: any = this.patient[patientKey]; if (patient && patient.chatWS) { - this.closeStatus[name + id + date + index + 'chatWS'] = true - patient.chatWS.close() - delete patient['chatWS'] + this.closeStatus[`${patientKey}chatWS`] = true; + this.setActiveDisconnect(index, true); // 标记为主动断开 + patient.chatWS.close(); + delete patient.chatWS; + this.clearReconnectTimer(index); // 强制清除重连定时器 } }, sendMedicine(args: { diff --git a/src/views/remote-manage/part/remote-item-part.vue b/src/views/remote-manage/part/remote-item-part.vue index bc1d8f9..3a966bf 100644 --- a/src/views/remote-manage/part/remote-item-part.vue +++ b/src/views/remote-manage/part/remote-item-part.vue @@ -138,7 +138,8 @@ function subscribeVital() { function wsClose() { remoteWsStore.vitalOnclose(remoteTask.value.patient, remoteTask.value.patientId, remoteTask.value.date, props.index, () => { setTimeout(() => { - ElMessage.info('远程查看' + (props.index + 1) + ', 生命体征数据连接断开,正在尝试重连……') + // ElMessage.info('远程查看' + (props.index + 1) + ', 生命体征数据连接断开,正在尝试重连……') + ElMessage.info('远程查看,生命体征数据连接断开,正在尝试重连……') initData() }, 3000) }) diff --git a/src/views/remote-manage/part/remote-part.vue b/src/views/remote-manage/part/remote-part.vue index 62018bd..ad4d38f 100644 --- a/src/views/remote-manage/part/remote-part.vue +++ b/src/views/remote-manage/part/remote-part.vue @@ -221,7 +221,8 @@ function initData() { } // 创建定时器并保存到store const timerId = setTimeout(() => { - ElMessage.info(`远程查看${currentIndex + 1},生命体征数据连接断开,正在尝试重连……`) + // ElMessage.info(`远程查看${currentIndex + 1},生命体征数据连接断开,正在尝试重连……`) + ElMessage.info(`远程查看,生命体征数据连接断开,正在尝试重连……`) remoteWsStore.createVital( remoteItem.value.patient, remoteItem.value.patientId,