#update 地图统一推送功能

This commit is contained in:
2026-09-14 10:37:27 +08:00
parent cfd2906b71
commit 4f7f32eeb2
17 changed files with 206 additions and 34 deletions

View File

@@ -9,8 +9,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@Component
@@ -25,6 +24,8 @@ public class MiddleGlobalMemory {
*/
public static Vertx vertx;
//TODO 推送全局的时候需要 判断是不是单个的控制
public static ConcurrentHashMap<Long, Map<String, WebSocket>> mapPushMap = new ConcurrentHashMap<>();
// 存储所有在线WebSocket连接
// key: test:web:token
public static final ConcurrentHashMap<String, WebSocket> onlineSockets = new ConcurrentHashMap<>();
@@ -84,6 +85,7 @@ public class MiddleGlobalMemory {
.findFirst().orElse(null);
}
public static String findNettyClient(NettyClient nettyClient) {
return nettyClientMap.entrySet().stream()
.filter(entry -> Objects.equals(entry.getValue(), nettyClient))
@@ -91,5 +93,38 @@ public class MiddleGlobalMemory {
.findFirst().orElse(null);
}
public static void addMapSocket(Long siteId, String token, WebSocket webSocket) {
mapPushMap
.computeIfAbsent(siteId, k -> new ConcurrentHashMap<>())
.put(token, webSocket);
}
public static void removeByWebSocket(WebSocket webSocket) {
if (webSocket == null) {
return;
}
mapPushMap.forEach((siteId, socketMap) -> {
socketMap.entrySet().removeIf(entry ->
entry.getValue() == webSocket
);
// siteId 下没有连接了,删除整个 siteId
if (socketMap.isEmpty()) {
mapPushMap.remove(siteId, socketMap);
}
});
}
public static List<WebSocket> getMapSockets(Long siteId) {
Map<String, WebSocket> map = mapPushMap.get(siteId);
if (map == null || map.isEmpty()) {
return Collections.emptyList();
}
return new ArrayList<>(map.values());
}
}