2016-01-28 59 views
0

我剛開始使用Netty。它看起來很有趣,但我遇到了一個問題。客戶端連接後,應收到一條消息,但沒有發生。出於某種原因,連接工作正常;它說Channel ConnectedNetty 5 messageReceived沒有被調用

服務器:

this.bossgroup = new NioEventLoopGroup(); 
    ServerBootstrap bootstrap = new ServerBootstrap() 
      .group(bossgroup) 
      .channel(NioServerSocketChannel.class) 
      .childHandler(new ChannelInitializer<SocketChannel>() { 

       @Override 
       protected void initChannel(SocketChannel ch) throws Exception { 
        ch.pipeline().addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())) 
          .addLast("decoder", new StringDecoder()) 
          .addLast("encoder", new StringEncoder()) 
          .addLast("timer", new ReadTimeoutHandler(10)) 
          .addLast("handler", new ChannelHandler()); 
       } 
      }); 

的ChannelHandler:

public class ChannelHandler extends SimpleChannelInboundHandler<String> { 

    @Override 
    public void channelActive(ChannelHandlerContext ctx) throws Exception { 
     System.out.println("[+] Channel connected: " + ctx.channel().remoteAddress()); 
     new ClientHandler(ctx.channel()); 
    } 

    @Override 
    public void channelInactive(ChannelHandlerContext ctx) throws Exception { 
     System.out.println("Channel disconnected: " + ctx.channel().remoteAddress() + " [-]"); 
     ctx.close(); 
    } 

    @Override 
    protected void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception { 
     System.out.println(ctx.channel().remoteAddress() + ": " + msg); 
    } 

    @Override 
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { 
     System.out.println("Exception caught, closing channel." + cause); 
     ctx.close(); 
    } 
} 
+0

是客戶端成功連接後發送的任何消息?在頻道主動(...)方法我相信你應該刪除新的頻道處理程序創建並嘗試。 –

+0

嘿,是的,客戶端發送成功連接後的消息。 – Shrekt

+0

你可以嘗試通過在chanelActive(...)方法中刪除新的channelhandler創建嗎?不知道爲什麼它是需要的。你可以發佈客戶端代碼嗎? –

回答

0

解題者是: ChannelInboundHandlerAdapter

代替:SimpleChannelInboundHandler

相關問題