From 105091b17e5a277b51d4d50cbfe018d813af2787 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Thu, 4 May 2023 14:07:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E8=81=8A=E5=A4=A9=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shuyu/advance_02_protocol/TestHttp.java | 4 +--- .../advance_02_protocol/TestRedisClient.java | 2 +- .../custom/TestMessageCodec.java | 4 +++- .../custom/message/LoginRequestMessage.java | 1 - .../custom/message/Message.java | 17 +++-------------- .../java/com/kwan/shuyu/server/ChatServer.java | 6 +++--- 6 files changed, 11 insertions(+), 23 deletions(-) diff --git a/netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/TestHttp.java b/netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/TestHttp.java index fc2c9c6..04a2c60 100644 --- a/netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/TestHttp.java +++ b/netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/TestHttp.java @@ -37,9 +37,7 @@ public class TestHttp { ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG)); // 做http服务器端 , 解码器对请求进行解码 ch.pipeline().addLast(new HttpServerCodec()); - /** - * SimpleChannelInboundHandler 可以指定 只关心某一种类型的Handler - */ + //SimpleChannelInboundHandler 可以指定 只关心某一种类型的Handler ch.pipeline().addLast(new SimpleChannelInboundHandler() { // 或者:HttpContent @Override protected void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) throws Exception { diff --git a/netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/TestRedisClient.java b/netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/TestRedisClient.java index 693df1e..024db9e 100644 --- a/netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/TestRedisClient.java +++ b/netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/TestRedisClient.java @@ -62,7 +62,7 @@ public class TestRedisClient { public void channelActive(ChannelHandlerContext ctx) throws Exception { // 分配 ByteBuf final ByteBuf buf = ctx.alloc().buffer(); - buf.writeBytes("*3".getBytes()); + buf.writeBytes("*3".getBytes());//数组长度 buf.writeBytes(LINE); buf.writeBytes("$3".getBytes()); buf.writeBytes(LINE); diff --git a/netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/custom/TestMessageCodec.java b/netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/custom/TestMessageCodec.java index 2ef3503..e7320bd 100644 --- a/netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/custom/TestMessageCodec.java +++ b/netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/custom/TestMessageCodec.java @@ -38,9 +38,10 @@ public class TestMessageCodec { protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { // 这里的 Object msg 已经 【被 MessageCodecSharable 解码成了 Message类型了】 LoginRequestMessage loginRequestMessage = (LoginRequestMessage) msg; - System.out.println(loginRequestMessage.getNickname() + "==================" + loginRequestMessage.getPassword()); + System.out.println(loginRequestMessage.getUsername() + "==================" + loginRequestMessage.getPassword()); } }; + //相当于服务端 final EmbeddedChannel channel = new EmbeddedChannel( LOGGIN_HANDLER, // 【移动到流水线的最上方 可以 打印出 半包情况】 new LengthFieldBasedFrameDecoder( @@ -48,6 +49,7 @@ public class TestMessageCodec { MESSAGE_CODEC, channelInboundHandler ); + //相当于客户端 LoginRequestMessage message = new LoginRequestMessage("张三", "123456"); final ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(); // 新建一个buf new MessageCodec().encode(null, message, buf); // 【编码 入站】 diff --git a/netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/custom/message/LoginRequestMessage.java b/netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/custom/message/LoginRequestMessage.java index 6026a20..5ca261a 100644 --- a/netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/custom/message/LoginRequestMessage.java +++ b/netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/custom/message/LoginRequestMessage.java @@ -11,7 +11,6 @@ import lombok.ToString; public class LoginRequestMessage extends Message { private String username; private String password; - private String nickname; public LoginRequestMessage() { } diff --git a/netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/custom/message/Message.java b/netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/custom/message/Message.java index 19a96c1..0a788f5 100644 --- a/netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/custom/message/Message.java +++ b/netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/custom/message/Message.java @@ -19,6 +19,8 @@ public abstract class Message implements Serializable { public abstract int getMessageType(); + private static final Map> messageClasses = new HashMap<>(); + public static final int LoginRequestMessage = 0; public static final int LoginResponseMessage = 1; public static final int ChatRequestMessage = 2; @@ -33,22 +35,9 @@ public abstract class Message implements Serializable { public static final int GroupChatResponseMessage = 11; public static final int GroupMembersRequestMessage = 12; public static final int GroupMembersResponseMessage = 13; - private static final Map> messageClasses = new HashMap<>(); + static { messageClasses.put(LoginRequestMessage, LoginRequestMessage.class); -/* messageClasses.put(LoginResponseMessage, LoginResponseMessage.class); - messageClasses.put(ChatRequestMessage, ChatRequestMessage.class); - messageClasses.put(ChatResponseMessage, ChatResponseMessage.class); - messageClasses.put(GroupCreateRequestMessage, GroupCreateRequestMessage.class); - messageClasses.put(GroupCreateResponseMessage, GroupCreateResponseMessage.class); - messageClasses.put(GroupJoinRequestMessage, GroupJoinRequestMessage.class); - messageClasses.put(GroupJoinResponseMessage, GroupJoinResponseMessage.class); - messageClasses.put(GroupQuitRequestMessage, GroupQuitRequestMessage.class); - messageClasses.put(GroupQuitResponseMessage, GroupQuitResponseMessage.class); - messageClasses.put(GroupChatRequestMessage, GroupChatRequestMessage.class); - messageClasses.put(GroupChatResponseMessage, GroupChatResponseMessage.class); - messageClasses.put(GroupMembersRequestMessage, GroupMembersRequestMessage.class); - messageClasses.put(GroupMembersResponseMessage, GroupMembersResponseMessage.class);*/ } } diff --git a/netty-04-chat/src/main/java/com/kwan/shuyu/server/ChatServer.java b/netty-04-chat/src/main/java/com/kwan/shuyu/server/ChatServer.java index 5cf561b..6ba57ba 100644 --- a/netty-04-chat/src/main/java/com/kwan/shuyu/server/ChatServer.java +++ b/netty-04-chat/src/main/java/com/kwan/shuyu/server/ChatServer.java @@ -62,14 +62,14 @@ public class ChatServer { IdleStateEvent event = (IdleStateEvent) evt; // 是否 读超时 if (event.state() == IdleState.READER_IDLE) { - log.debug("==============================已经12秒没读到数据了!===================================="); + log.debug("===已经12秒没读到数据了!==="); ctx.channel().close(); } if (event.state() == IdleState.WRITER_IDLE) { - log.debug("==============================写超时!===================================="); + log.debug("===写超时!========="); } if (event.state() == IdleState.ALL_IDLE) { - log.debug("==============================读写超时!===================================="); + log.debug("===读写超时!========="); } } }); -- GitLab