简体   繁体   English

Netty 5消息收到未被调用

[英]Netty 5 messageReceived not being called

I just started using Netty. 我刚开始使用Netty。 It looks very interesting but I encountered a problem. 看起来很有趣,但是我遇到了问题。 After the client is connected, a message should be received but that's not happening. 客户端连接后,应该会收到一条消息,但是没有发生。 For some reason connection works fine; 由于某种原因,连接工作正常; it says Channel Connected . 它说Channel Connected

Server: 服务器:

    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 : 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();
    }
}

Solved By: ChannelInboundHandlerAdapter 解决者:ChannelInboundHandlerAdapter

Instead Of:SimpleChannelInboundHandler 代替:SimpleChannelInboundHandler

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM