Преглед изворни кода

晋掌柜webSocket通信添加状态

helixiao пре 2 година
родитељ
комит
36d8e072c2

+ 20 - 0
src/main/java/com/ydtech/modules/admin/controller/websocket/WebSocketServer.java

@@ -53,6 +53,7 @@ public class WebSocketServer {
             // 在线数加1
             onlineCount.incrementAndGet();
         }
+        editStatus(userId);
         log.info("用户连接:" + userId + ",当前在线人数为:" + onlineCount);
         sendMessage("连接成功");
     }
@@ -72,6 +73,7 @@ public class WebSocketServer {
 
     @OnClose
     public void onClose(@PathParam("userId") String userId, Session session) {
+        delStatus(userId);
         sessionClientMap.remove(userId);
         log.info("断联成功!");
     }
@@ -81,6 +83,24 @@ public class WebSocketServer {
         log.info("websocket异常,错误信息:{}", throwable.getMessage());
     }
 
+    private void delStatus(String userId) {
+        SysGeographyPositionService sysGeographyPositionService= SpringContextUtils.getBean(SysGeographyPositionService.class);
+        SysGeographyPosition sysGeographyPosition = sysGeographyPositionService.getById(userId);
+        if (sysGeographyPosition != null) {
+            sysGeographyPosition.setStatus(0);
+            sysGeographyPositionService.updateById(sysGeographyPosition);
+        }
+    }
+
+    private void editStatus(String userId) {
+        SysGeographyPositionService sysGeographyPositionService= SpringContextUtils.getBean(SysGeographyPositionService.class);
+        SysGeographyPosition sysGeographyPosition = sysGeographyPositionService.getById(userId);
+        if (sysGeographyPosition != null) {
+            sysGeographyPosition.setStatus(1);
+            sysGeographyPositionService.updateById(sysGeographyPosition);
+        }
+    }
+
     public void sendMessage(String message) {
         this.session.getAsyncRemote().sendText(message);
     }

+ 4 - 0
src/main/java/com/ydtech/modules/admin/model/SysGeographyPosition.java

@@ -36,6 +36,10 @@ public class SysGeographyPosition implements Serializable {
      * 地址
      */
     private String address;
+    /**
+     * 地址
+     */
+    private Integer status;
     /**
      * 创建时间
      */