From d2fa8a8cf9bdc7231841625e96efd827cf075f4f Mon Sep 17 00:00:00 2001 From: Bunny <1319900154@qq.com> Date: Thu, 23 May 2024 22:43:28 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=96=B0=E5=A2=9E):=20:rocket:=20netty?= =?UTF-8?q?=E5=BC=80=E5=A7=8B=E7=9A=84=E5=AE=A2=E6=88=B7=E7=AB=AF=E5=92=8C?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/netty/demo1/StartClient01.java | 8 ++--- .../service/netty/demo1/StartServer01.java | 32 +++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/netty/service/src/main/java/cn/bunny/service/netty/demo1/StartClient01.java b/netty/service/src/main/java/cn/bunny/service/netty/demo1/StartClient01.java index f1dc2ab..5aa4dcd 100644 --- a/netty/service/src/main/java/cn/bunny/service/netty/demo1/StartClient01.java +++ b/netty/service/src/main/java/cn/bunny/service/netty/demo1/StartClient01.java @@ -3,9 +3,8 @@ package cn.bunny.service.netty.demo1; import io.netty.bootstrap.Bootstrap; import io.netty.channel.ChannelInitializer; import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; -import io.netty.handler.codec.string.StringDecoder; +import io.netty.handler.codec.string.StringEncoder; import java.net.InetSocketAddress; @@ -19,14 +18,15 @@ public class StartClient01 { // 添加 事件循环 .group(new NioEventLoopGroup()) // 选择客户端 channel 实现 - .channel(NioServerSocketChannel.class) + .channel(NioSocketChannel.class) // 添加处理器 .handler(new ChannelInitializer() { // 连接后被调用 @Override protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception { - nioSocketChannel.pipeline().addLast(new StringDecoder()); + nioSocketChannel.pipeline().addLast(new StringEncoder()); } + }) .connect(new InetSocketAddress("localhost", 8080)) .sync() diff --git a/netty/service/src/main/java/cn/bunny/service/netty/demo1/StartServer01.java b/netty/service/src/main/java/cn/bunny/service/netty/demo1/StartServer01.java index 947e6af..d7a085b 100644 --- a/netty/service/src/main/java/cn/bunny/service/netty/demo1/StartServer01.java +++ b/netty/service/src/main/java/cn/bunny/service/netty/demo1/StartServer01.java @@ -8,9 +8,9 @@ import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.string.StringDecoder; +import lombok.extern.slf4j.Slf4j; -import static java.lang.StringTemplate.STR; - +@Slf4j public class StartServer01 { /* 服务器端处理内容 @@ -26,22 +26,22 @@ public class StartServer01 { .childHandler( // 代码客户端读写通道 添加别的 handler new ChannelInitializer() { - @Override - protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception { - // 添加解码,将传输过来的 ByteBuffer 转为字符串 - nioSocketChannel.pipeline().addLast(new StringDecoder()); - // 自定处理器 - nioSocketChannel.pipeline().addLast(new ChannelInboundHandlerAdapter() { - // 发生读事件 @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - // 打印上一步转化好的字符串 - System.out.println(STR."消息为\{msg}"); - super.channelRead(ctx, msg); + protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception { + // 添加解码,将传输过来的 ByteBuffer 转为字符串 + nioSocketChannel.pipeline().addLast(new StringDecoder()); + // 自定处理器 + nioSocketChannel.pipeline().addLast(new ChannelInboundHandlerAdapter() { + // 发生读事件 + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + // 打印上一步转化好的字符串 + System.out.println(STR."消息为\{msg}"); + log.debug("消息:{}", msg); + } + }); } - }); - } - }) + }) // 绑定端口 .bind(8080); }