From 64b4fe31a327a160529546076d26797bbcb1a022 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E5=B0=8F=E5=A2=A8?= <2291200076@qq.com> Date: Tue, 25 Apr 2023 02:04:40 +0800 Subject: [PATCH] =?UTF-8?q?bugfix=20=E4=BF=AE=E5=A4=8D=E5=BC=80=E5=90=AF?= =?UTF-8?q?=E5=A4=9A=E4=B8=AA=E9=97=A8=E7=A6=81websocket=E9=A2=91=E7=B9=81?= =?UTF-8?q?=E6=96=AD=E5=BC=80=E9=97=AE=E9=A2=98=EF=BC=9Bbugfix=E4=BB=85?= =?UTF-8?q?=E6=89=AB=E6=8F=8F=E9=97=A8=E7=A6=81=E6=98=BE=E7=A4=BA=E6=8F=90?= =?UTF-8?q?=E7=A4=BA=E7=AA=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../static/guard/assets/js/websocket.js | 26 ++++++++++++++----- .../access/controller/WebSocketServer.java | 6 ++--- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 88a38cf..54a9335 100644 --- a/README.md +++ b/README.md @@ -333,7 +333,7 @@ const baseUrl = "/" 修改 `backend/microservice-provider-access-8002/src/main/resources/static/access/assets/js/websocket.js` 文件 ``` -window.wsUrl = 'ws://【⚠此处修改为你的业务域名】/access/websocket/1'; +window.wsUrl = 'ws://【⚠此处修改为你的业务域名】/access/websocket/'; ``` > 注意,如果使用了 SSL 证书,那么 ws:// 要换成 wss:// diff --git a/backend/microservice-gateway/src/main/resources/static/guard/assets/js/websocket.js b/backend/microservice-gateway/src/main/resources/static/guard/assets/js/websocket.js index e3a2de9..8774a8d 100644 --- a/backend/microservice-gateway/src/main/resources/static/guard/assets/js/websocket.js +++ b/backend/microservice-gateway/src/main/resources/static/guard/assets/js/websocket.js @@ -2,10 +2,11 @@ * FIXME 环境配置 * * window.wsUrl - * - 线上环境:'wss://epp.only4.work/access/websocket/1' - * - 开发环境:'ws://127.0.0.1:80/access/websocket/1'; 'ws://127.0.0.1:8002/access/websocket/1'; + * - 线上环境:'wss://epp.only4.work/access/websocket/' + * - 开发环境:'ws://127.0.0.1:80/access/websocket/' + * 'ws://127.0.0.1:8002/access/websocket/' */ -window.wsUrl = 'ws://127.0.0.1:80/access/websocket/1'; +window.wsUrl = 'ws://127.0.0.1:80/access/websocket/'; window.ws = null; // WebSocket 实例对象 @@ -15,9 +16,20 @@ window.ws = null; // WebSocket 实例对象 return } + function getWsUrl() { + function getUUID() { + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { + var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); + return v.toString(16); + }); + } + + return `${window.wsUrl}${getUUID()}` + } + function createConn() { // 创建webscoket 对象 - const ws = new WebSocket(window.wsUrl) + const ws = new WebSocket(getWsUrl() /* window.wsUrl */) // 执行上面的语句之后,客户端就会与服务器进行连接 // readyState返回当前实例对象的当前状态 @@ -47,8 +59,10 @@ window.ws = null; // WebSocket 实例对象 switch (result.action) { case 'onscan': case 'onopen': - console.log(result.action) - window.showResult(result.action) + if (window.currentGate && result.gateId === window.currentGate.id) { + console.log(result.action) + window.showResult(result.action) + } break; default: console.log("switch=>default", result.action) diff --git a/backend/microservice-provider-access-8002/src/main/java/com/cxyxiaomo/epp/access/controller/WebSocketServer.java b/backend/microservice-provider-access-8002/src/main/java/com/cxyxiaomo/epp/access/controller/WebSocketServer.java index 2d0904c..5ef2eb1 100644 --- a/backend/microservice-provider-access-8002/src/main/java/com/cxyxiaomo/epp/access/controller/WebSocketServer.java +++ b/backend/microservice-provider-access-8002/src/main/java/com/cxyxiaomo/epp/access/controller/WebSocketServer.java @@ -18,7 +18,7 @@ public class WebSocketServer { private Session session; // session集合,存放对应的session - private static ConcurrentHashMap sessionPool = new ConcurrentHashMap<>(); + private static ConcurrentHashMap sessionPool = new ConcurrentHashMap<>(); // concurrent包的线程安全Set,用来存放每个客户端对应的WebSocket对象。 private static CopyOnWriteArraySet webSocketSet = new CopyOnWriteArraySet<>(); @@ -30,7 +30,7 @@ public class WebSocketServer { * @param userId 用户ID */ @OnOpen - public void onOpen(Session session, @PathParam(value = "userId") Integer userId) { + public void onOpen(Session session, @PathParam(value = "userId") String userId) { log.info("WebSocket建立连接中,连接用户ID:{}", userId); try { Session historySession = sessionPool.get(userId); @@ -84,7 +84,7 @@ public class WebSocketServer { * @param userId 用户ID * @param message 发送的消息 */ - public static void sendMessageByUser(Integer userId, String message) { + public static void sendMessageByUser(String userId, String message) { log.info("用户ID:" + userId + ",推送内容:" + message); Session session = sessionPool.get(userId); try {