Просмотр исходного кода

解决心跳重连问题,解决查看自己病历的问题

xiaochan 11 месяцев назад
Родитель
Сommit
a127a53deb

+ 14 - 4
thyy-socket/src/main/java/org/thyy/socket/config/CloseCodes.java

@@ -1,15 +1,14 @@
 package org.thyy.socket.config;
 
-import lombok.Getter;
+import cn.hutool.core.text.StrFormatter;
 import org.springframework.web.socket.CloseStatus;
 
 /**
  * 3000 ~ 4000 之间
  */
-@Getter
 public enum CloseCodes {
 
-    EDITING_IN_PROGRESS(3001, "还有人在编辑无法连接"),
+    EDITING_IN_PROGRESS(3001, "用户{}正在编辑,无法连接服务器。"),
 
     KICKING_PEOPLE(3002, "已有医生在踢人,无法操作。"),
 
@@ -17,10 +16,21 @@ public enum CloseCodes {
 
     FORCED_KICK_OUT(3004, "其他用户强制踢出");
 
+    private final Integer code;
+    private final String reason;
     private final CloseStatus status;
 
     CloseCodes(int code, String reason) {
-        this.status = new CloseStatus(code, reason);
+        this.code = code;
+        this.reason = reason;
+        this.status = new CloseStatus(code);
+    }
+
+    public CloseStatus getStrFormatterStatus(Object... values) {
+        if (values == null || values.length == 0) {
+            return status;
+        }
+        return new CloseStatus(this.code, StrFormatter.format(this.reason, values));
     }
 
 }

+ 9 - 2
thyy-socket/src/main/java/org/thyy/socket/service/Business.java

@@ -1,5 +1,7 @@
 package org.thyy.socket.service;
 
+import cn.hutool.core.text.StrFormatter;
+import cn.hutool.json.JSONObject;
 import org.springframework.web.socket.TextMessage;
 import org.springframework.web.socket.WebSocketSession;
 import org.thyy.socket.config.CloseCodes;
@@ -23,12 +25,17 @@ public interface Business {
         }
     }
 
-    static void closeReason(WebSocketSession session, CloseCodes codes) {
+    static void closeReason(WebSocketSession session, CloseCodes codes, Object... value) {
         if (session == null) return;
         try {
-            session.close(codes.getStatus());
+            session.close(codes.getStrFormatterStatus(value));
         } catch (IOException ignored) {
         }
     }
 
+    static <T> T getAttribute(WebSocketSession session, String key) {
+        return (T) session.getAttributes().get(key);
+    }
+
+
 }

+ 10 - 2
thyy-socket/src/main/java/org/thyy/socket/service/emr/EmrDocument.java

@@ -26,13 +26,21 @@ public class EmrDocument implements Business {
         if (session1 == null) {
             mapCenter.getDocument().put(sid, session);
         } else {
-            Business.closeReason(session, CloseCodes.EDITING_IN_PROGRESS);
+            JSONObject userInfo = (JSONObject) session1.getAttributes().get("userInfo");
+            String name = "";
+            if (userInfo != null) {
+                name = userInfo.getString("name");
+            }
+            Business.closeReason(session, CloseCodes.EDITING_IN_PROGRESS, name);
         }
     }
 
     @Override
     public void onClose(WebSocketSession session, String sid) {
-        mapCenter.getDocument().remove(sid);
+        WebSocketSession webSocketSession = mapCenter.getDocument().get(sid);
+        if (webSocketSession != null && webSocketSession.getId().equals(session.getId())) {
+            mapCenter.getDocument().remove(sid);
+        }
     }
 
     @Override

+ 5 - 6
thyy-socket/src/main/java/org/thyy/socket/service/emr/EmrEditor.java

@@ -1,5 +1,6 @@
 package org.thyy.socket.service.emr;
 
+import cn.hutool.core.text.StrFormatter;
 import com.alibaba.fastjson2.JSONObject;
 import lombok.AllArgsConstructor;
 import lombok.Builder;
@@ -39,6 +40,9 @@ public class EmrEditor implements Business {
     public EmrEditor() {
         function.put("USER_INFO", (params) -> {
             params.getSession().getAttributes().put("userInfo", params.getMessage());
+            Business.send(params.getSession(), """
+                    {"code":"pong"}
+                    """);
         });
 
         function.put("VIEW_DOCUMENT", (params) -> {
@@ -80,12 +84,7 @@ public class EmrEditor implements Business {
         String roomId = split[0] + "_" + split[1];
         List<WebSocketSession> sessions = ROOM.computeIfAbsent(roomId, k -> new ArrayList<>());
         sessions.add(session);
-        Business.send(session, """
-                {
-                    "code" : "ping",
-                    "data":  null
-                }
-                """);
+        Business.send(session, StrFormatter.format("{\"code\": \"ID\", \"data\": \"{}\"}", session.getId()));
     }
 
     @Override