From b85c3ebf7e846353cb553aaee828b6ad0b8c0cf7 Mon Sep 17 00:00:00 2001 From: zhangdahai112 Date: Thu, 30 Jun 2022 16:04:51 +0800 Subject: [PATCH] =?UTF-8?q?fix(UI=20=E8=87=AA=E5=8A=A8=E5=8C=96):=20cloud?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E6=9C=89=E6=97=B6=E5=80=99=E5=88=B7=E4=B8=8D?= =?UTF-8?q?=E5=87=BA=E6=9D=A5=E6=89=A7=E8=A1=8C=E6=97=B6=E9=97=B4=E8=BE=83?= =?UTF-8?q?=E9=95=BF=E7=9A=84=E6=8A=A5=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1014412 --user=张大海 【UI测试】github#13927,ui-场景执行关闭了性能测试模式,执行页面任务等很久就不出报告,查看任务中心实际已经执行完了 https://www.tapd.cn/55049933/s/1192594 --- .../websocket/c/to/c/WebSocketUtils.java | 5 ++- .../automation/report/SysnApiReportDetail.vue | 38 ++++++++++++++++++- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/backend/src/main/java/io/metersphere/websocket/c/to/c/WebSocketUtils.java b/backend/src/main/java/io/metersphere/websocket/c/to/c/WebSocketUtils.java index c1702c673a..c0eeef1ce1 100644 --- a/backend/src/main/java/io/metersphere/websocket/c/to/c/WebSocketUtils.java +++ b/backend/src/main/java/io/metersphere/websocket/c/to/c/WebSocketUtils.java @@ -6,6 +6,7 @@ import io.metersphere.websocket.c.to.c.util.MsgDto; import javax.websocket.RemoteEndpoint; import javax.websocket.Session; import java.util.Map; +import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; public class WebSocketUtils { @@ -27,8 +28,8 @@ public class WebSocketUtils { // 单用户推送 public static void sendMessageSingle(MsgDto dto) { - sendMessage(ONLINE_USER_SESSIONS.get(dto.getReportId()), dto.getContent()); - sendMessage(ONLINE_USER_SESSIONS.get(dto.getToReport()), dto.getContent()); + sendMessage(ONLINE_USER_SESSIONS.get(Optional.ofNullable(dto.getReportId()).orElse("")), dto.getContent()); + sendMessage(ONLINE_USER_SESSIONS.get(Optional.ofNullable(dto.getToReport()).orElse("")), dto.getContent()); } // 全用户推送 diff --git a/frontend/src/business/components/api/automation/report/SysnApiReportDetail.vue b/frontend/src/business/components/api/automation/report/SysnApiReportDetail.vue index 4404ec75a9..6e26a31d4b 100644 --- a/frontend/src/business/components/api/automation/report/SysnApiReportDetail.vue +++ b/frontend/src/business/components/api/automation/report/SysnApiReportDetail.vue @@ -151,7 +151,11 @@ export default { infoDb: Boolean, debug: Boolean, scenario: {}, - scenarioId: String + scenarioId: String, + isUi: { + type: Boolean, + default: false + } }, methods: { initTree() { @@ -300,7 +304,12 @@ export default { } const uri = protocol + window.location.host + "/ws/" + this.reportId; this.messageWebSocket = new WebSocket(uri); + //初始化心跳 目前ui的机制容易使得 ws 超时 + if (this.isUi) { + this.initHeartBeat(); + } this.messageWebSocket.onmessage = this.onMessage; + this.messageWebSocket.onerror = this.cleanHeartBeat; }, getReport() { let url = "/api/scenario/report/get/" + this.reportId; @@ -458,6 +467,7 @@ export default { if (e.data && e.data.indexOf("MS_TEST_END") !== -1) { this.getReport(); this.messageWebSocket.close(); + this.cleanHeartBeat(); this.$EventBus.$emit('hide', this.scenarioId); this.$emit('refresh', this.debugResult); } @@ -490,6 +500,32 @@ export default { } } } + }, + websocketKey(){ + return "ui_ws_" + this.reportId; + }, + initHeartBeat() { + if (!window.localStorage.getItem(this.websocketKey())) { + window.localStorage.setItem(this.websocketKey(), this.reportId); + window.heartBeatHandle = setInterval(this.heartBeat, 30000); + } else { + this.cleanHeartBeat(); + this.initHeartBeat(); + } + }, + cleanHeartBeat() { + if (window.heartBeatHandle) { + clearInterval(window.heartBeatHandle); + if (window.localStorage.getItem(this.websocketKey())) + window.localStorage.removeItem(this.websocketKey()); + } + }, + heartBeat() { + let msg = { + reportId: this.reportId, + content: "i'm alive" + }; + this.messageWebSocket.send(JSON.stringify(msg)); } }, computed: {