Browse Source

Merge remote-tracking branch 'origin/master_exclusive_shop_20250718' into master_exclusive_shop_20250718

yuhongqi 11 hours ago
parent
commit
e83e1f84c9

+ 27 - 3
fs-live-socket/src/main/java/com/fs/live/websocket/service/WebSocketServer.java

@@ -28,6 +28,7 @@ import java.io.IOException;
 import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.ThreadLocalRandom;
 import java.util.concurrent.TimeUnit;
 
 import static com.fs.common.constant.LiveKeysConstant.*;
@@ -415,17 +416,40 @@ public class WebSocketServer {
 
         room.forEach((k, v) -> {
             if (v.isOpen()) {
-                v.getAsyncRemote().sendText(message);
+                sendWithRetry(v,message,7);
             }
         });
         adminRoom.forEach(v -> {
             if (v.isOpen()) {
-                v.getAsyncRemote().sendText(message);
+                sendWithRetry(v,message,7);
             }
         });
     }
 
-
+    private void sendWithRetry(Session session, String message, int maxRetries) {
+        int attempts = 0;
+        while (attempts < maxRetries) {
+            try {
+                if(session.isOpen()) {
+                    session.getAsyncRemote().sendText(message);
+                }
+                return;  // 发送成功,退出
+            } catch (Exception e) {
+                if (e.getMessage() != null && e.getMessage().contains("TEXT_FULL_WRITING")) {
+                    attempts++;
+                    try {
+                        TimeUnit.MILLISECONDS.sleep(ThreadLocalRandom.current().nextInt(5, 100));
+                    } catch (InterruptedException ie) {
+                        Thread.currentThread().interrupt();
+                        break;
+                    }
+                } else {
+                    throw e;
+                }
+            }
+        }
+        log.info("超过重试次数, 消息 {}",message);
+    }
 
     public void handleAutoTask(LiveAutoTask task) {
         SendMsgVo msg = new SendMsgVo();