From 05c1b69b9a694cf99e9f07f12ebf034cb2450f5f Mon Sep 17 00:00:00 2001 From: mitao <2763622819@qq.com> Date: 星期一, 10 二月 2025 10:15:38 +0800 Subject: [PATCH] websocket改造 --- ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/websocket/WebSocketServer.java | 9 ++++++--- 1 files changed, 6 insertions(+), 3 deletions(-) diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/websocket/WebSocketServer.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/websocket/WebSocketServer.java index ee679e8..edd6125 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/websocket/WebSocketServer.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/websocket/WebSocketServer.java @@ -1,5 +1,6 @@ package com.ruoyi.system.websocket; +import com.ruoyi.common.core.utils.JwtUtils; import java.util.concurrent.Semaphore; import javax.websocket.OnClose; import javax.websocket.OnError; @@ -18,7 +19,7 @@ * @作者 ruoyi */ @Component -@ServerEndpoint("/websocket/message/{clientType}") +@ServerEndpoint("/websocket/message/{clientType}/{accessToken}") public class WebSocketServer { private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketServer.class); @@ -26,7 +27,8 @@ private static Semaphore socketSemaphore = new Semaphore(socketMaxOnlineCount); @OnOpen - public void onOpen(Session session, @PathParam("clientType") Integer clientType) + public void onOpen(Session session, @PathParam("clientType") Integer clientType, + @PathParam("accessToken") String accessToken) throws Exception { boolean semaphoreFlag = false; semaphoreFlag = SemaphoreUtils.tryAcquire(socketSemaphore); @@ -36,7 +38,8 @@ "当前在线人数超过限制数:" + socketMaxOnlineCount); session.close(); } else { - WebSocketUsers.put(session.getId(), session, clientType); + String userId = JwtUtils.getUserId(accessToken); + WebSocketUsers.put(session.getId(), session, clientType, Long.parseLong(userId)); if (clientType == 1) { LOGGER.info("\n 用户端建立连接 - {}", session); -- Gitblit v1.7.1