Java messageReceived(netty)未调用

Java messageReceived(netty)未调用,java,netty,protocol-buffers,Java,Netty,Protocol Buffers,我的消息来源 TestMessage是我的protobuf对象 @Override public ChannelPipeline getPipeline() { ChannelPipeline next = Channels.pipeline(); next.addLast("frameDecoder", new ProtobufVarint32FrameDecoder()); next.addLa

我的消息来源 TestMessage是我的protobuf对象

        @Override
        public ChannelPipeline getPipeline() {
            ChannelPipeline next = Channels.pipeline();

        next.addLast("frameDecoder", new ProtobufVarint32FrameDecoder());
            next.addLast("protobufDecoder", new ProtobufDecoder(TestMessage.getDefaultInstance()));
        next.addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender());
        next.addLast("protobufEncoder", new ProtobufEncoder());


            return next;
        }

@Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
        Channel ch = e.getChannel();
        if (ch.isOpen()) {

            TestMessage req = (TestMessage) e.getMessage();
            System.out.println(req.getMessage());
            ch.close();

        }
    }
发送


我有个问题。messageReceived()在哪里

Protobufvarint32帧解码器,ProtobufDecoder,Protobufvarint32长字段预编码器,ProtobufEncoder

有四个班在netty。但是messageReceived()方法必须位于CustomHandler类中。班级呢

必须添加ChannelPipeline的下一个实例

ex)如果我为messageReceived()创建ProtobufHandler类(扩展SimpleChannelHandler)


抱歉,我的英语水平有限。

我一直在使用Netty 5.0.0,并测试GitHub中的一些聊天示例,这些示例是为使用较旧的Netty版本而开发的。这些示例使用客户端中的以下代码发送字符串:

channel.write(msg+“\r\n”)

我当前的Netty库从未调用过messageReceived函数。但是,在我将客户端代码更改为flush string之后,它开始被调用,如下所示:

channel.writeAndFlush(msg+“\r\n”)

Socket fromserver = new Socket("localhost", 7283);

        PrintWriter out = new PrintWriter(fromserver.getOutputStream(), true);

        TestMessage.Builder message = TestMessage.newBuilder();

        message.setMessage("message .....");

        message.build();

        out.println(message);

        out.close();
        fromserver.close();
ChannelPipeline next = Channels.pipeline();
next.addLast("frameDecoder", new ProtobufVarint32FrameDecoder());
next.addLast("protobufDecoder", new ProtobufDecoder(TestMessage.getDefaultInstance()));
next.addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender());
next.addLast("protobufEncoder", new ProtobufEncoder());

next.addLast("protobufHandler", new ProtobufHandler()); //add this.