ezhizao_zx il y a 3 mois
Parent
commit
ec8f8e073a

+ 1 - 0
src/main/java/cn/ezhizao/framework/task/RyTask.java

@@ -134,6 +134,7 @@ public class RyTask {
         for (Map.Entry<Long, List<BizMessage>> entry : groupById.entrySet()) {
             WebSocketUser.sendInfo(String.valueOf(entry.getValue().size()), String.valueOf(entry.getKey()));
         }
+        WebSocketUser.sendEmptyMessage(messages.stream().map(BizMessage::getToUserId).collect(Collectors.toList()));
     }
 
     /** 原始二维码生成方法,生成JSON格式

+ 14 - 1
src/main/java/cn/ezhizao/project/websocket/WebSocketUser.java

@@ -15,6 +15,7 @@ import javax.websocket.server.PathParam;
 import javax.websocket.server.ServerEndpoint;
 import java.io.IOException;
 import java.util.Date;
+import java.util.List;
 import java.util.concurrent.ConcurrentHashMap;
 
 //前端请求的路径
@@ -30,7 +31,7 @@ public class WebSocketUser {
     /**
      * concurrent包的线程安全,用来存放每个客户端对应的MyWebSocket对象。
      */
-    private static ConcurrentHashMap<String, WebSocketUser> webSocketMap = new ConcurrentHashMap<>();
+    public static ConcurrentHashMap<String, WebSocketUser> webSocketMap = new ConcurrentHashMap<>();
     /**
      * 与某个客户端的连接会话,需要通过它来给客户端发送数据
      */
@@ -158,6 +159,18 @@ public class WebSocketUser {
         }
     }
 
+    public static void sendEmptyMessage(List<Long> userIds) {
+        webSocketMap.forEach((key, value) -> {
+            if (userIds.stream().noneMatch(v -> String.valueOf(v).equals(key))) {
+                try {
+                    value.sendMessage(0 + "");
+                } catch (IOException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+        });
+    }
+
     public static synchronized int getOnlineCount() {
         return onlineCount;
     }