diff --git a/detection/src/main/java/com/njcn/gather/detection/util/socket/WebServiceManager.java b/detection/src/main/java/com/njcn/gather/detection/util/socket/WebServiceManager.java new file mode 100644 index 00000000..100cd49f --- /dev/null +++ b/detection/src/main/java/com/njcn/gather/detection/util/socket/WebServiceManager.java @@ -0,0 +1,35 @@ +package com.njcn.gather.detection.util.socket; + +import io.netty.channel.Channel; +import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * @Description: webSocket存储的通道 + * @Author: wr + * @Date: 2024/12/11 13:04 + */ +public class WebServiceManager { + private static final Map userSessions = new ConcurrentHashMap<>(); + + public static void addUser(String userId, Channel channel) { + userSessions.put(userId, channel); + } + + public static void removeUser(String userId) { + userSessions.remove(userId); + } + + public static Channel getChannelByUserId(String userId) { + return userSessions.get(userId); + } + + public static void sendMsg(String msg) { + Channel userId = userSessions.get("userId"); + TextWebSocketFrame wd1 = new TextWebSocketFrame(msg); + userId.writeAndFlush(wd1); + } +} + \ No newline at end of file