From 8d9eca3f5e9f884a14f41d04d1898e2ee7bf9078 Mon Sep 17 00:00:00 2001
From: mitao <2763622819@qq.com>
Date: 星期二, 11 六月 2024 16:08:08 +0800
Subject: [PATCH] 1.提交【管理后台】-营销管理-积分管理相关接口 2.修改websocket代码,增加用户端、拍卖师端类型区分

---
 ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/websocket/WebSocketServer.java |   43 +++++++++----------------------------------
 1 files changed, 9 insertions(+), 34 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 aa555c0..33ae939 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
@@ -7,6 +7,7 @@
 import javax.websocket.OnMessage;
 import javax.websocket.OnOpen;
 import javax.websocket.Session;
+import javax.websocket.server.PathParam;
 import javax.websocket.server.ServerEndpoint;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -15,83 +16,57 @@
 /**
  * websocket 消息处理
  *
- * @author ruoyi
+ * @作者 ruoyi
  */
 @Component
-@ServerEndpoint("/websocket/message")
+@ServerEndpoint("/websocket/message/{clientType}")
 public class WebSocketServer {
 
-    /**
-     * WebSocketServer 日志控制器
-     */
     private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketServer.class);
-
-    /**
-     * 默认最多允许同时在线人数100
-     */
-    public static int socketMaxOnlineCount = 100;
-
+    public static int socketMaxOnlineCount = 1000;
     private static Semaphore socketSemaphore = new Semaphore(socketMaxOnlineCount);
 
-    /**
-     * 连接建立成功调用的方法
-     */
     @OnOpen
-    public void onOpen(Session session) throws Exception {
+    public void onOpen(Session session, @PathParam("clientType") Integer clientType)
+            throws Exception {
         boolean semaphoreFlag = false;
-        // 尝试获取信号量
         semaphoreFlag = SemaphoreUtils.tryAcquire(socketSemaphore);
         if (!semaphoreFlag) {
-            // 未获取到信号量
             LOGGER.error("\n 当前在线人数超过限制数- {}", socketMaxOnlineCount);
             WebSocketUsers.sendMessageToUserByText(session,
                     "当前在线人数超过限制数:" + socketMaxOnlineCount);
             session.close();
         } else {
-            // 添加用户
-            WebSocketUsers.put(session.getId(), session);
+            WebSocketUsers.put(session.getId(), session, clientType);
             LOGGER.info("\n 建立连接 - {}", session);
             LOGGER.info("\n 当前人数 - {}", WebSocketUsers.getUsers().size());
             WebSocketUsers.sendMessageToUserByText(session, "连接成功");
         }
     }
 
-    /**
-     * 连接关闭时处理
-     */
     @OnClose
     public void onClose(Session session) {
         LOGGER.info("\n 关闭连接 - {}", session);
-        // 移除用户
         WebSocketUsers.remove(session.getId());
-        // 获取到信号量则需释放
         SemaphoreUtils.release(socketSemaphore);
     }
 
-    /**
-     * 抛出异常时处理
-     */
     @OnError
     public void onError(Session session, Throwable exception) throws Exception {
         if (session.isOpen()) {
-            // 关闭连接
             session.close();
         }
         String sessionId = session.getId();
         LOGGER.info("\n 连接异常 - {}", sessionId);
         LOGGER.info("\n 异常信息 - {}", exception);
-        // 移出用户
         WebSocketUsers.remove(sessionId);
-        // 获取到信号量则需释放
         SemaphoreUtils.release(socketSemaphore);
     }
 
-    /**
-     * 服务器接收到客户端消息时调用的方法
-     */
     @OnMessage
     public void onMessage(String message, Session session) {
-        String msg = message.replace("你", "我").replace("吗", "");
+        String msg = message.replace("你", "我").replace("吗", "").replace("PING", "PONG")
+                .replace("ping", "pong");
         WebSocketUsers.sendMessageToUserByText(session, msg);
     }
 }

--
Gitblit v1.7.1