feat(新增): 🚀 PipLineServer的链式传输

This commit is contained in:
bunny 2024-05-24 09:58:29 +08:00
parent 150a71a1e4
commit 9f4cdfb04f
1 changed files with 7 additions and 2 deletions

View File

@ -1,12 +1,15 @@
package cn.bunny.service.netty.demo5; package cn.bunny.service.netty.demo5;
import io.netty.bootstrap.ServerBootstrap; import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*; import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.nio.charset.Charset;
@Slf4j @Slf4j
public class PipLineServer { public class PipLineServer {
public static void main(String[] args) { public static void main(String[] args) {
@ -22,7 +25,9 @@ public class PipLineServer {
pipeline.addLast("h1", new ChannelInboundHandlerAdapter() { pipeline.addLast("h1", new ChannelInboundHandlerAdapter() {
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
log.info("第一个"); log.info("第一个");
super.channelRead(ctx, msg); ByteBuf buf = (ByteBuf) msg;
String name = buf.toString(Charset.defaultCharset());
super.channelRead(ctx, name);
} }
}); });
@ -38,7 +43,7 @@ public class PipLineServer {
pipeline.addLast("h3", new ChannelInboundHandlerAdapter() { pipeline.addLast("h3", new ChannelInboundHandlerAdapter() {
@Override @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
log.info("第三个"); log.info("第三个 是否拿到上一个参数:{}", msg);
super.channelRead(ctx, msg); super.channelRead(ctx, msg);
nioSocketChannel.writeAndFlush(ctx.alloc().buffer().writeBytes("服务".getBytes())); nioSocketChannel.writeAndFlush(ctx.alloc().buffer().writeBytes("服务".getBytes()));
} }