我创建了一个名为MessageHandler的类:

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ch.pipeline().addLast(
            new LoggingHandler(LogLevel.INFO),
            new GameServerHandler());
    ch.pipeline().addLast("protobufHandler", new MessageHandler());
}


另外,我添加了messageReceiver函数,无法按照文档中的说明覆盖它,因为它给了我一个错误:

public class MessageHandler extends SimpleChannelInboundHandler<Object> {
    // @Override
    public void messageReceived(ChannelHandlerContext ctx, Object msg) {
        System.out.println(msg);
        // super.messageReceived(ctx, msg);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    protected void channelRead0(ChannelHandlerContext arg0, Object arg1)
            throws Exception {
        // TODO Auto-generated method stub
    }
}


但是从不调用messageReceived函数:

INFO: [id: 0xbb603bfd, /127.0.0.1:54206 => /127.0.0.1:5000] ACTIVE
Oct 07, 2014 10:48:49 PM io.netty.handler.logging.LoggingHandler logMessage
INFO: [id: 0xbf711f5f, /127.0.0.1:54205 => /127.0.0.1:5000] RECEIVED(383B)
// Message printed by the Netty logger, not from my function.

最佳答案

我netty 4.x您需要重写并将System.out.println(...)内容放入其中的方法是channelRead0(...)。仅在netty 5中,它的messageReceived(...)。

关于java - messageReceived未调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26245059/

10-10 19:40