xiaochan 4 months ago
parent
commit
6c452ca01c

+ 2 - 1
thyy-socket/src/main/java/org/thyy/socket/ThyySocketApplication.java

@@ -2,10 +2,11 @@ package org.thyy.socket;
 
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
 import org.springframework.context.annotation.ComponentScan;
 
-@SpringBootApplication
 @ComponentScan("org.thyy.*")
+@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
 public class ThyySocketApplication {
     public static void main(String[] args) {
         SpringApplication.run(ThyySocketApplication.class, args);

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

@@ -1,11 +1,13 @@
 package org.thyy.socket.service;
 
+import com.alibaba.fastjson2.JSON;
 import com.alibaba.fastjson2.JSONObject;
 import org.springframework.web.socket.TextMessage;
 import org.springframework.web.socket.WebSocketSession;
 import org.thyy.socket.config.CloseCodes;
 
 import java.io.IOException;
+import java.util.function.Consumer;
 
 public interface Business {
 
@@ -46,4 +48,10 @@ public interface Business {
     default void sendMessage(JSONObject json) {
     }
 
+    static JSONObject attemptToConvertJson(String json, Consumer<JSONObject> consumer) {
+        JSONObject jsonObject = JSON.parseObject(json);
+        consumer.accept(jsonObject);
+        return null;
+    }
+
 }

+ 10 - 5
thyy-socket/src/main/java/org/thyy/socket/service/IntergrationPlatform/IntergrationPlatform.java

@@ -1,5 +1,6 @@
 package org.thyy.socket.service.IntergrationPlatform;
 
+import cn.hutool.core.util.StrUtil;
 import com.alibaba.fastjson2.JSONArray;
 import com.alibaba.fastjson2.JSONObject;
 import lombok.extern.slf4j.Slf4j;
@@ -54,6 +55,13 @@ public class IntergrationPlatform implements Business {
         if (data.equals("heart-beat")) {
             Business.send(session, data);
         }
+        Business.attemptToConvertJson(data, (value) -> {
+            if (value.getString("code").equals("getOnlineCount")) {
+                Business.send(session, StrUtil.format("""
+                        {"code": "onlineCount" , "message": {} }
+                        """, sessionContainer.size()));
+            }
+        });
     }
 
     @Override
@@ -96,11 +104,8 @@ public class IntergrationPlatform implements Business {
 
     private void executeSendMessage(String sid, String msg) {
         List<WebSocketSession> sessionList = sessionContainer.get(sid);
-        try {
-            for (WebSocketSession item : sessionList) {
-                item.sendMessage(new TextMessage(msg));
-            }
-        } catch (Exception ignored) {
+        for (WebSocketSession item : sessionList) {
+            Business.send(item, msg);
         }
     }
 }