#update 地图统一推送功能
This commit is contained in:
@@ -8,6 +8,8 @@ import lombok.Data;
|
||||
public class WebMessageBaseDTO {
|
||||
private MesType type;
|
||||
private String userName;
|
||||
private Long siteId;
|
||||
private Long orgId;
|
||||
private String deviceId;
|
||||
private String token;
|
||||
private String message;
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -64,6 +64,7 @@ public class WebsocketHandler extends WebSocketServer {
|
||||
releaseResource(key);
|
||||
logger.info("客户端断开连接: remoteAddress={}, code={}, reason={}, 当前在线数: {}", conn.getRemoteSocketAddress(), code, reason, MiddleGlobalMemory.onlineSockets.size());
|
||||
}
|
||||
MiddleGlobalMemory.removeByWebSocket(conn);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -77,22 +78,15 @@ public class WebsocketHandler extends WebSocketServer {
|
||||
String deviceId = webMessageBaseDTO.getDeviceId();
|
||||
MesType type = webMessageBaseDTO.getType();
|
||||
String key = userName + ":web" + ":" + token;
|
||||
if (MesType.AUTH.equals(type)) {
|
||||
if (MesType.mapPush.equals(type)) {
|
||||
Long siteId = webMessageBaseDTO.getSiteId();
|
||||
MiddleGlobalMemory.addMapSocket(siteId, token, conn);
|
||||
} else if (MesType.AUTH.equals(type)) {
|
||||
if (StringUtils.isEmpty(deviceId)) {
|
||||
return;
|
||||
}
|
||||
//todo 先关闭原先的
|
||||
// WebSocket oldWebsocket = MiddleGlobalMemory.onlineSockets.get(key);
|
||||
NettyClient client = MiddleGlobalMemory.nettyClientMap.get(key);
|
||||
|
||||
// if (oldWebsocket != null && oldWebsocket.isOpen()) {
|
||||
// oldWebsocket.close();
|
||||
// }
|
||||
// if (oldNettyClient != null && oldNettyClient.isConnected()) {
|
||||
// oldNettyClient.disconnect();
|
||||
// }
|
||||
MiddleGlobalMemory.onlineSockets.put(key, conn);
|
||||
//这里可以根据需要初始化Netty客户端
|
||||
if (client == null) {
|
||||
client = new NettyClient();
|
||||
}
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
package com.maibu.websocket;
|
||||
|
||||
import com.maibu.core.business.inter.DeviceWebSocketInterace;
|
||||
import com.maibu.core.business.inter.DeviceWebSocketInterface;
|
||||
import com.maibu.memory.MiddleGlobalMemory;
|
||||
|
||||
import org.java_websocket.WebSocket;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class WebsocketMesHandler implements DeviceWebSocketInterace {
|
||||
public class WebsocketMesHandler implements DeviceWebSocketInterface {
|
||||
|
||||
@Override
|
||||
public void sendWebsoketMes(String key, String data) {
|
||||
public void sendWebsocketMes(String key, String data) {
|
||||
MiddleGlobalMemory.onlineSockets.keySet().forEach(x -> {
|
||||
// key: test:web:token
|
||||
if (x.contains(key)) {
|
||||
@@ -21,4 +24,14 @@ public class WebsocketMesHandler implements DeviceWebSocketInterace {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMapWebsocketMes(Long siteId, String data) {
|
||||
List<WebSocket> webSocketList = MiddleGlobalMemory.getMapSockets(siteId);
|
||||
if (!CollectionUtils.isEmpty(webSocketList)) {
|
||||
webSocketList.forEach(webSocket -> {
|
||||
WebsocketHandler.sendMessageToClient(webSocket, data);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user