fix(接口测试): 修复接口调试偶发接收不到结果问题

https://github.com/metersphere/metersphere/issues/13725
This commit is contained in:
fit2-zhao 2022-05-19 12:09:28 +08:00 committed by 刘瑞斌
parent e0761b9ac0
commit 1b3152f8aa
2 changed files with 20 additions and 7 deletions

View File

@ -1,6 +1,7 @@
package io.metersphere.websocket.c.to.c; package io.metersphere.websocket.c.to.c;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import io.metersphere.utils.LoggerUtil;
import io.metersphere.websocket.c.to.c.util.MsgDto; import io.metersphere.websocket.c.to.c.util.MsgDto;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -21,7 +22,11 @@ public class IndexWebSocket {
@OnOpen @OnOpen
public void openSession(@PathParam("reportId") String reportId, Session session) { public void openSession(@PathParam("reportId") String reportId, Session session) {
WebSocketUtils.ONLINE_USER_SESSIONS.put(reportId, session); WebSocketUtils.ONLINE_USER_SESSIONS.put(reportId, session);
log.info("客户端: [" + reportId + "] : 连接成功!" + WebSocketUtils.ONLINE_USER_SESSIONS.size()); RemoteEndpoint.Async async = session.getAsyncRemote();
if (async != null) {
async.sendText("CONN_SUCCEEDED");
}
LoggerUtil.info("客户端: [" + reportId + "] : 连接成功!" + WebSocketUtils.ONLINE_USER_SESSIONS.size());
} }
/** /**
@ -29,7 +34,7 @@ public class IndexWebSocket {
*/ */
@OnMessage @OnMessage
public void onMessage(@PathParam("reportId") String reportId, String message) { public void onMessage(@PathParam("reportId") String reportId, String message) {
log.info("服务器收到:[" + reportId + "] : " + message); LoggerUtil.info("服务器收到:[" + reportId + "] : " + message);
MsgDto dto = JSON.parseObject(message, MsgDto.class); MsgDto dto = JSON.parseObject(message, MsgDto.class);
dto.setContent(dto.getContent()); dto.setContent(dto.getContent());
WebSocketUtils.sendMessageSingle(dto); WebSocketUtils.sendMessageSingle(dto);
@ -42,7 +47,7 @@ public class IndexWebSocket {
public void onClose(@PathParam("reportId") String reportId, Session session) throws IOException { public void onClose(@PathParam("reportId") String reportId, Session session) throws IOException {
//当前的Session 移除 //当前的Session 移除
WebSocketUtils.ONLINE_USER_SESSIONS.remove(reportId); WebSocketUtils.ONLINE_USER_SESSIONS.remove(reportId);
log.info("[" + reportId + "] : 断开连接!" + WebSocketUtils.ONLINE_USER_SESSIONS.size()); LoggerUtil.info("[" + reportId + "] : 断开连接!" + WebSocketUtils.ONLINE_USER_SESSIONS.size());
//并且通知其他人当前用户已经断开连接了 //并且通知其他人当前用户已经断开连接了
session.close(); session.close();
} }

View File

@ -36,7 +36,7 @@ export default {
watch: { watch: {
reportId() { reportId() {
// //
this.socketSyncResult(); this.socketSync();
}, },
isStop() { isStop() {
if (!this.isStop && this.websocket && this.websocket.close instanceof Function) { if (!this.isStop && this.websocket && this.websocket.close instanceof Function) {
@ -45,7 +45,7 @@ export default {
} }
}, },
methods: { methods: {
socketSyncResult() { socketSync() {
let protocol = "ws://"; let protocol = "ws://";
if (window.location.protocol === 'https:') { if (window.location.protocol === 'https:') {
protocol = "wss://"; protocol = "wss://";
@ -53,10 +53,18 @@ export default {
const uri = protocol + window.location.host + "/ws/" + this.reportId; const uri = protocol + window.location.host + "/ws/" + this.reportId;
this.websocket = new WebSocket(uri); this.websocket = new WebSocket(uri);
this.websocket.onmessage = this.onMessages; this.websocket.onmessage = this.onMessages;
// this.websocket.onerror = this.onError;
this.websocket.onopen = this.run(); },
onError(){
this.$emit('runRefresh', "");
this.$error("The connection is abnormal, please check the environment configuration");
}, },
onMessages(e) { onMessages(e) {
//
if(e && e.data === "CONN_SUCCEEDED"){
this.run();
}
if (e.data && e.data.startsWith("result_")) { if (e.data && e.data.startsWith("result_")) {
try { try {
let data = e.data.substring(7); let data = e.data.substring(7);