Ver código fonte

空指针判断

xiaochan 4 meses atrás
pai
commit
28910808a4

+ 1 - 1
pom.xml

@@ -18,7 +18,7 @@
     <properties>
         <java.version>21</java.version>
         <thyy-utils>0.0.6</thyy-utils>
-        <thyy-socket>0.1.1</thyy-socket>
+        <thyy-socket>0.1.3</thyy-socket>
         <thyy-archive>0.1.1</thyy-archive>
         <thyy-haikang>0.0.5</thyy-haikang>
         <thyy-emr-query>1.5.1</thyy-emr-query>

+ 7 - 0
thyy-socket/src/main/java/org/thyy/socket/config/SocketService.java

@@ -57,6 +57,13 @@ public class SocketService implements WebSocketHandler {
 
     @Override
     public void handleTransportError(WebSocketSession session, Throwable exception) {
+        String businessStr = (String) session.getAttributes().get("business");
+        String sid = (String) session.getAttributes().get("sid");
+        Business business = businessMap.get(businessStr);
+        if (business == null) {
+            return;
+        }
+        business.onError(session, sid, exception);
         log.error("Transport error for session {}", session.getId(), exception);
     }
 

+ 4 - 0
thyy-socket/src/main/java/org/thyy/socket/service/Business.java

@@ -24,6 +24,10 @@ public interface Business {
     default void onMessage(WebSocketSession session, String sid, String data) {
     }
 
+    default void onError(WebSocketSession session, String sid, Throwable ex) {
+
+    }
+
     static void send(WebSocketSession session, String data) {
         if (session == null) return;
         try {

+ 9 - 0
thyy-socket/src/main/java/org/thyy/socket/service/mainbusiness/IntergrationPlatform.java

@@ -30,6 +30,9 @@ public class IntergrationPlatform implements Business {
         } else {
             String currentIp = (String) session.getAttributes().get("ip");
             for (WebSocketSession item : sessionList) {
+                if (item == null) {
+                    continue;
+                }
                 if (!item.getAttributes().get("ip").equals(currentIp)) {
                     Business.send(item, SINGLE_LOGIN);
                 }
@@ -49,6 +52,12 @@ public class IntergrationPlatform implements Business {
         log.info("有一连接关闭:{},当前在线人数为:{}", sid, sessionContainer.size());
     }
 
+
+    @Override
+    public void onError(WebSocketSession session, String sid, Throwable ex) {
+        onClose(session, sid);
+    }
+
     @Override
     public void onMessage(WebSocketSession session, String sid, String data) {
         if (data.equals("heart-beat")) {