在阅读 Netty 教程时,我发现了一个关于如何集成 Netty 和Google Protocol Buffers的简单描述。我已经开始研究它的示例(因为文档中没有更多信息)并编写了一个简单的应用程序,例如示例本地时间应用程序。但是这个例子是在 PipeFactory 类中使用静态初始化,例如:
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.handler.codec.protobuf.ProtobufDecoder;
import org.jboss.netty.handler.codec.protobuf.ProtobufEncoder;
import org.jboss.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import org.jboss.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
import static org.jboss.netty.channel.Channels.pipeline;
/**
* @author sergiizagriichuk
*/
class ProtoCommunicationClientPipeFactory implements ChannelPipelineFactory {
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline p = pipeline();
p.addLast("frameDecoder", new ProtobufVarint32FrameDecoder());
p.addLast("protobufDecoder", new ProtobufDecoder(Communication.DataMessage.getDefaultInstance()));
p.addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender());
p.addLast("protobufEncoder", new ProtobufEncoder());
p.addLast("handler", new ProtoCommunicationClientHandler());
return p;
}
}
(请看一下 line p.addLast("protobufDecoder", new ProtobufDecoder(Communication.DataMessage.getDefaultInstance()));
)并且只能为ClientBootstrap
类创建一个工厂(据我所知),我的意思是bootstrap.setPipelineFactory()
方法。所以,在这种情况下,我可以使用一条 消息发送到服务器并使用一条消息从服务器接收,这对我来说很糟糕,我认为不仅仅是对我来说:(我怎样才能使用不同的消息来往和来自一个连接? 也许我可以创造一些protobufDecoder
这样的
p.addLast("protobufDecoder", new ProtobufDecoder(Communication.DataMessage.getDefaultInstance()));
p.addLast("protobufDecoder", new ProtobufDecoder(Communication.TestMessage.getDefaultInstance()));
p.addLast("protobufDecoder", new ProtobufDecoder(Communication.SrcMessage.getDefaultInstance()));
还是其他技术?非常感谢。