车车 1 年之前
父节点
当前提交
110d11e354

+ 5 - 14
ktg-admin/src/main/java/com/ktg/web/controller/iscs/SysTeamController.java

@@ -3,30 +3,21 @@ package com.ktg.web.controller.iscs;
 
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.ktg.common.annotation.Log;
 import com.ktg.common.core.controller.BaseController;
+import com.ktg.common.core.domain.AjaxResult;
+import com.ktg.common.enums.BusinessType;
 import com.ktg.common.pojo.CommonResult;
 import com.ktg.common.utils.StringUtils;
 import com.ktg.system.domain.SysTeam;
 import com.ktg.system.service.ISysTeamService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
-import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.Parameter;
 import io.swagger.v3.oas.annotations.Parameters;
-import io.swagger.v3.oas.annotations.tags.Tag;
-import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.PutMapping;
-import org.springframework.web.bind.annotation.DeleteMapping;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
-import com.ktg.common.annotation.Log;
-import com.ktg.common.core.domain.AjaxResult;
-import com.ktg.common.enums.BusinessType;
+import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.web.bind.annotation.*;
 
 /**
  * 小组Controller

+ 34 - 1
ktg-framework/src/main/java/com/ktg/framework/websocket/WebSocketServer.java

@@ -1,5 +1,6 @@
 package com.ktg.framework.websocket;
 
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.Semaphore;
 import javax.websocket.OnClose;
 import javax.websocket.OnError;
@@ -26,7 +27,7 @@ import org.springframework.stereotype.Component;
 
 /**
  * websocket 消息处理
- * 
+ *
  * @author ruoyi
  */
 @Component
@@ -46,12 +47,22 @@ public class WebSocketServer
 
     private static Semaphore socketSemaphore = new Semaphore(socketMaxOnlineCount);
 
+    /**静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。*/
+    private static int onlineCount = 0;
+
+    /**
+     * concurrent包的线程安全set,用来存放每个客户端对应的MyWebSocket对象
+     */
+    private static ConcurrentHashMap<String,WebSocketServer> webSocketMap = new ConcurrentHashMap();
+
+
     /**
      * 连接建立成功调用的方法
      */
     @OnOpen
     public void onOpen(Session session,@PathParam("token") String token) throws Exception
     {
+        LOGGER.info("[连接token:{}] 建立连接, 当前连接数:{}", token, webSocketMap.size());
         boolean semaphoreFlag = false;
         //身份验证
         if(!StringUtils.isNotNull(token)){
@@ -78,6 +89,14 @@ public class WebSocketServer
         }
         else
         {
+            if (webSocketMap.containsKey(token)) {
+                webSocketMap.remove(token);
+                webSocketMap.put(token,this);
+            } else {
+                webSocketMap.put(token,this);
+                addOnlineCount();
+            }
+
             // 添加用户
             WebSocketUsers.put(user.getUsername(), session);
             LOGGER.info("\n 建立连接 - {}", session);
@@ -139,4 +158,18 @@ public class WebSocketServer
         LOGGER.debug("\n 收到客户端发送的消息 - {}", message);
     }
 
+    /**
+     * 当前连接数加一
+     */
+    public static synchronized void addOnlineCount() {
+        WebSocketServer.onlineCount++;
+    }
+
+    /**
+     * 当前连接数减一
+     */
+    public static synchronized void subOnlineCount() {
+        WebSocketServer.onlineCount--;
+    }
+
 }