From 5575818556096be1dadaf5ff356b5db4c832aaa2 Mon Sep 17 00:00:00 2001 From: Helius <wangdoubleone@gmail.com> Date: Wed, 26 May 2021 19:27:09 +0800 Subject: [PATCH] modify --- src/main/java/com/xcong/excoin/netty/handler/WebSocketServerHandler.java | 71 ++++++++++++++++++----------------- 1 files changed, 36 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/xcong/excoin/netty/handler/WebSocketServerHandler.java b/src/main/java/com/xcong/excoin/netty/handler/WebSocketServerHandler.java index 2ceb827..a6d7bef 100644 --- a/src/main/java/com/xcong/excoin/netty/handler/WebSocketServerHandler.java +++ b/src/main/java/com/xcong/excoin/netty/handler/WebSocketServerHandler.java @@ -38,14 +38,14 @@ @ChannelHandler.Sharable public class WebSocketServerHandler extends ChannelInboundHandlerAdapter { - private final ConcurrentMap<String, Integer> pingTimes = new ConcurrentHashMap<>(); +// private final ConcurrentMap<String, Integer> pingTimes = new ConcurrentHashMap<>(); private static final int MAX_UN_REC_PING_TIMES = 3; private WebSocketServerHandshaker handshaker; - @Resource(name = "msgDispatch") - private MsgDispatch msgDispatch; +// @Resource(name = "msgDispatch") +// private MsgDispatch msgDispatch; @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { @@ -76,7 +76,7 @@ if (content.contains(Contans.HEART_BEAT)) { resetTimes(ctx.channel()); } else { - this.msgDispatch.webSocketDispatch(ctx, content); +// this.msgDispatch.webSocketDispatch(ctx, content); } } catch (ClassCastException e) { content = ((CloseWebSocketFrame) frame).reasonText(); @@ -88,40 +88,40 @@ @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { log.info("[触发器触发]"); - if (evt instanceof IdleStateEvent) { - IdleStateEvent event = (IdleStateEvent) evt; - if (event.state() == IdleState.READER_IDLE) { - - } else if (event.state() == IdleState.WRITER_IDLE) { - /*写超时*/ - ctx.channel().writeAndFlush(NettyTools.webSocketBytes(Contans.HEART_BEAT)); - Integer times = pingTimes.get(ctx.channel().id().asShortText()); - if (times == null) { - times = 0; - } - /*读超时*/ - log.info("===服务端===({}写超时, {})", ctx.channel().id().asShortText(), times); - // 失败计数器次数大于等于3次的时候,关闭链接,等待client重连 - if (times >= MAX_UN_REC_PING_TIMES) { - log.info("===服务端===(写超时,关闭chanel)"); - // 连续超过N次未收到client的ping消息,那么关闭该通道,等待client重连 - ctx.channel().close(); - } else { - // 失败计数器加1 - times++; - pingTimes.remove(ctx.channel().id().asShortText()); - pingTimes.put(ctx.channel().id().asShortText(), times); - } - } else if (event.state() == IdleState.ALL_IDLE) { - /*总超时*/ - System.out.println("===服务端===(ALL_IDLE 总超时)"); - } - } +// if (evt instanceof IdleStateEvent) { +// IdleStateEvent event = (IdleStateEvent) evt; +// if (event.state() == IdleState.READER_IDLE) { +// +// } else if (event.state() == IdleState.WRITER_IDLE) { +// /*写超时*/ +// ctx.channel().writeAndFlush(NettyTools.webSocketBytes(Contans.HEART_BEAT)); +// Integer times = pingTimes.get(ctx.channel().id().asShortText()); +// if (times == null) { +// times = 0; +// } +// /*读超时*/ +// log.info("===服务端===({}写超时, {})", ctx.channel().id().asShortText(), times); +// // 失败计数器次数大于等于3次的时候,关闭链接,等待client重连 +// if (times >= MAX_UN_REC_PING_TIMES) { +// log.info("===服务端===(写超时,关闭chanel)"); +// // 连续超过N次未收到client的ping消息,那么关闭该通道,等待client重连 +// ctx.channel().close(); +// } else { +// // 失败计数器加1 +// times++; +// pingTimes.remove(ctx.channel().id().asShortText()); +// pingTimes.put(ctx.channel().id().asShortText(), times); +// } +// } else if (event.state() == IdleState.ALL_IDLE) { +// /*总超时*/ +// System.out.println("===服务端===(ALL_IDLE 总超时)"); +// } +// } } private void resetTimes(Channel channel) { - pingTimes.remove(channel.id().asShortText()); - pingTimes.put(channel.id().asShortText(), 0); +// pingTimes.remove(channel.id().asShortText()); +// pingTimes.put(channel.id().asShortText(), 0); } @Override @@ -132,6 +132,7 @@ private void handlerWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) { + System.out.println(111111111); // 判断是否关闭链路的指令 if (frame instanceof CloseWebSocketFrame) { handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain()); -- Gitblit v1.9.1