From b801497a33d98c33250a146308897eaca255dfed Mon Sep 17 00:00:00 2001 From: wr <1754607820@qq.com> Date: Wed, 11 Dec 2024 20:41:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A3=80=E6=B5=8B=E6=A8=A1=E5=9D=97=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../util/socket/WebServiceManager.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 detection/src/main/java/com/njcn/gather/detection/util/socket/WebServiceManager.java 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