diff --git a/storeroom/pom.xml b/storeroom/pom.xml
index f934c72..973c5b8 100644
--- a/storeroom/pom.xml
+++ b/storeroom/pom.xml
@@ -27,13 +27,20 @@
io.netty
netty-all
- 4.1.33.Final
+ 4.1.77.Final
+
com.ejlchina
okhttps-fastjson
3.5.2
+
+ cn.hutool
+ hutool-all
+ 5.8.3
+
+
\ No newline at end of file
diff --git a/storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket/NettyConfig.java b/storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket/NettyConfig.java
index 8ec75f0..c9e7299 100644
--- a/storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket/NettyConfig.java
+++ b/storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket/NettyConfig.java
@@ -11,33 +11,32 @@ public class NettyConfig {
/**
- * 定义一个channel,管理所有channel
+ * 定义一个channel组,管理所有的channel
+ * GlobalEventExecutor.INSTANCE 是全局的事件执行器,是一个单例
*/
private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
-
/**
- * 存放用户信息,发送指定用户
+ * 存放用户与Chanel的对应信息,用于给指定用户发送消息
*/
- private static ConcurrentHashMap userChannelMap = new ConcurrentHashMap<>();
-
- private NettyConfig() {
+ private static ConcurrentHashMap userChannelMap = new ConcurrentHashMap<>();
- }
+ private NettyConfig() {}
/**
- * 获取用户组
- * @return /
+ * 获取channel组
+ * @return
*/
- public static ChannelGroup getChannelGroup(){
+ public static ChannelGroup getChannelGroup() {
return channelGroup;
}
/**
* 获取用户channel map
- * @return /
+ * @return
*/
public static ConcurrentHashMap getUserChannelMap(){
return userChannelMap;
}
+
}
diff --git a/storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket/NettyServer.java b/storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket/NettyServer.java
index bbbf216..4f1e716 100644
--- a/storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket/NettyServer.java
+++ b/storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket/NettyServer.java
@@ -91,6 +91,8 @@ public class NettyServer {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
+
+
// 心跳检测(一般情况第一个设置,如果超时了,则会调用userEventTriggered方法,且会告诉你超时的类型)
ch.pipeline().addLast(new IdleStateHandler(readerIdleTime, writerIdleTime, allIdleTime, TimeUnit.MINUTES));
// 流水线管理通道中的处理程序(Handler),用来处理业务
diff --git a/storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket/ServerChannelCache.java b/storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket/ServerChannelCache.java
new file mode 100644
index 0000000..56b5e31
--- /dev/null
+++ b/storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket/ServerChannelCache.java
@@ -0,0 +1,50 @@
+package com.storeroom.modules.storeroom3d.config.websoket;
+
+import org.springframework.stereotype.Component;
+import io.netty.channel.Channel;
+import io.netty.util.AttributeKey;
+
+import java.util.HashMap;
+import java.util.concurrent.ConcurrentHashMap;
+
+@Component
+public class ServerChannelCache {
+
+
+ private static final ConcurrentHashMap> CACHE_MAP = new ConcurrentHashMap<>();
+ private static final AttributeKey CHANNEL_ATTR_KEY = AttributeKey.valueOf("test");
+
+ public String getCacheId(Channel channel) {
+ return channel.attr(CHANNEL_ATTR_KEY).get();
+ }
+
+ public void add(String cacheId, Channel channel) {
+ channel.attr(CHANNEL_ATTR_KEY).set(cacheId);
+ HashMap hashMap = CACHE_MAP.get(cacheId);
+ if (hashMap == null) {
+ hashMap = new HashMap<>();
+ }
+ hashMap.put(channel.id().asShortText(), channel);
+ CACHE_MAP.put(cacheId, hashMap);
+ }
+
+ public HashMap get(String cacheId) {
+ if (cacheId == null) {
+ return null;
+ }
+ return CACHE_MAP.get(cacheId);
+ }
+
+ public void remove(Channel channel) {
+ String cacheId = getCacheId(channel);
+ if (cacheId == null) {
+ return;
+ }
+ HashMap hashMap = CACHE_MAP.get(cacheId);
+ if (hashMap == null) {
+ hashMap = new HashMap<>();
+ }
+ hashMap.remove(channel.id().asShortText());
+ CACHE_MAP.put(cacheId, hashMap);
+ }
+}
diff --git a/storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket/WebSocketHandler.java b/storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket/WebSocketHandler.java
index 6d25642..29b0b2d 100644
--- a/storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket/WebSocketHandler.java
+++ b/storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket/WebSocketHandler.java
@@ -7,6 +7,7 @@ import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.AttributeKey;
+import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@@ -14,11 +15,17 @@ import org.springframework.stereotype.Component;
@Component
@ChannelHandler.Sharable
+@RequiredArgsConstructor
public class WebSocketHandler extends SimpleChannelInboundHandler{
private static final Logger log = LoggerFactory.getLogger(WebSocketHandler.class);
+ /**
+ * 一旦连接第一个被执行
+ * @param ctx
+ * @throws Exception
+ */
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
log.info("handlerAdded 被调用"+ctx.channel().id().asLongText());
@@ -26,7 +33,12 @@ public class WebSocketHandler extends SimpleChannelInboundHandler queryAllAlarm() {
+ public ApiResponse