From 17f9ecee29e12025aa6cd716c5cc6e7d7f707333 Mon Sep 17 00:00:00 2001 From: chendaofei <857448963@qq.com> Date: Thu, 16 Jan 2025 16:14:55 +0800 Subject: [PATCH] =?UTF-8?q?webSocket=E5=BF=83=E8=B7=B3=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../util/socket/WebServiceManager.java | 5 -- .../util/socket/web/WebSocketHandler.java | 63 ++++++++++++++----- .../util/socket/web/WebSocketInitializer.java | 4 ++ 3 files changed, 53 insertions(+), 19 deletions(-) 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 index c486a259..c21d84fa 100644 --- 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 @@ -24,15 +24,10 @@ 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) { - String id = userSessions.get(userId).id().toString(); - userSessions.remove(userId); - } public static void removeChannel(String channelId) { // 遍历并删除 diff --git a/detection/src/main/java/com/njcn/gather/detection/util/socket/web/WebSocketHandler.java b/detection/src/main/java/com/njcn/gather/detection/util/socket/web/WebSocketHandler.java index 1316eba5..5d96ff60 100644 --- a/detection/src/main/java/com/njcn/gather/detection/util/socket/web/WebSocketHandler.java +++ b/detection/src/main/java/com/njcn/gather/detection/util/socket/web/WebSocketHandler.java @@ -1,11 +1,17 @@ package com.njcn.gather.detection.util.socket.web; +import com.njcn.gather.detection.pojo.param.PreDetectionParam; +import com.njcn.gather.detection.util.socket.CnSocketUtil; +import com.njcn.gather.detection.util.socket.SocketManager; import com.njcn.gather.detection.util.socket.WebServiceManager; +import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; +import io.netty.handler.timeout.IdleStateEvent; +import io.netty.util.CharsetUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -22,12 +28,15 @@ import org.springframework.stereotype.Component; @Slf4j public class WebSocketHandler extends SimpleChannelInboundHandler { + private int times; + private final static String QUESTION_MARK = "?"; private final static String EQUAL_TO = "="; @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - System.out.println("服务端通道已建立" + ctx.channel().remoteAddress()); + System.out.println("webSocket服务端通道已建立" + ctx.channel().remoteAddress()); + super.channelActive(ctx); } @@ -49,7 +58,11 @@ public class WebSocketHandler extends SimpleChannelInboundHandler>>" + ctx.channel().id()); -// //已经10秒钟没有读时间了 -// if (event.state().equals(IdleState.READER_IDLE)){ -// // 心跳包丢失,10秒没有收到客户端心跳 (断开连接) -// ctx.channel().close().sync(); -// System.out.println("已与 "+ctx.channel().remoteAddress()+" 断开连接"); -// } -// } + IdleStateEvent event = (IdleStateEvent) evt; + String eventDesc = null; + switch (event.state()) { + case READER_IDLE: + eventDesc = "读空闲"; + System.out.println(ctx.channel().remoteAddress() + "发生超时事件--" + eventDesc); + times++; + if (times > 3) { + System.out.println("空闲次数超过三次 关闭连接"); + ctx.channel().close(); + } + break; + case WRITER_IDLE: + eventDesc = "写空闲"; + break; + case ALL_IDLE: + eventDesc = "读写空闲"; + break; + } + + + //super.userEventTriggered(ctx, evt); + } /** diff --git a/detection/src/main/java/com/njcn/gather/detection/util/socket/web/WebSocketInitializer.java b/detection/src/main/java/com/njcn/gather/detection/util/socket/web/WebSocketInitializer.java index ad78729d..af248c13 100644 --- a/detection/src/main/java/com/njcn/gather/detection/util/socket/web/WebSocketInitializer.java +++ b/detection/src/main/java/com/njcn/gather/detection/util/socket/web/WebSocketInitializer.java @@ -10,9 +10,12 @@ import io.netty.handler.codec.http.HttpResponseDecoder; import io.netty.handler.codec.http.HttpServerCodec; import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler; import io.netty.handler.stream.ChunkedWriteHandler; +import io.netty.handler.timeout.IdleStateHandler; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; +import java.util.concurrent.TimeUnit; + /** * @Description: webSocket服务端自定义配置 * @Author: wr @@ -44,6 +47,7 @@ public class WebSocketInitializer extends ChannelInitializer { * 这个时候我们只需要访问 ws://127.0.0.1:7777/hello 就可以了 * 这个handler是将http协议升级为websocket 并且使用 101 作为响应码 * */ + pipeline.addLast(new IdleStateHandler(10, -1, -1, TimeUnit.SECONDS)); pipeline.addLast(webSocketHandler); pipeline.addLast(new WebSocketServerProtocolHandler("/hello"));