netty channelRead 未自動執行


 1 ServerBootstrap bootstrap = new ServerBootstrap();
 2         try {
 3             bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
 4                     .childHandler(new ChannelInitializer() {
 5                         @Override
 6                         protected void initChannel(Channel ch) throws Exception {
 7                             ch.pipeline().addLast(
 8                                     new HttpServerCodec(),new HttpObjectAggregator(LOCALPORT), // 將多個消息轉換為單一的一個FullHttpRequest
 9                                     new HttpRequestDecoder(),
10                                     new ServcerHandler()    // 自己實現的Handler
11                             );
12                         }
13                     }).childOption(ChannelOption.AUTO_READ, false)
14                     .bind(LOCALPORT).sync().channel().closeFuture().sync();

因為設置了

childOption(ChannelOption.AUTO_READ, false)

所以在執行完

public void channelActive(ChannelHandlerContext ctx)

方法后,不會自動執行

public void channelRead(ChannelHandlerContext ctx, Object msg) 方法;

需要在channelActive中添加這行語句才會調用channelRead方法:

ctx.channel().read();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM