@ChannelHandler.Sharable public class ProtobufEncoderNano extends MessageToMessageEncoder<MessageNano>
MessageNano编码为ByteBuf 。
TCP / IP的典型设置是:
ChannelPipeline pipeline = ...;
// Decoders
pipeline.addLast("frameDecoder",
new LengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4));
pipeline.addLast("protobufDecoder",
new ProtobufDecoderNano(MyMessage.getDefaultInstance()));
// Encoder
pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
pipeline.addLast("protobufEncoder", new ProtobufEncoderNano());
然后您可以使用MyMessage而不是ByteBuf作为消息:
void channelRead(ChannelHandlerContext ctx, Object msg) {
MyMessage req = (MyMessage) msg;
MyMessage res = MyMessage.newBuilder().setText(
"Did you say '" + req.getText() + "'?").build();
ch.write(res);
}
ChannelHandler.Sharable| Constructor and Description |
|---|
ProtobufEncoderNano() |
| Modifier and Type | Method and Description |
|---|---|
protected void |
encode(ChannelHandlerContext ctx, MessageNano msg, java.util.List<java.lang.Object> out)
从一条消息编码到另一条消息。
|
acceptOutboundMessage, writebind, close, connect, deregister, disconnect, flush, readensureNotSharable, exceptionCaught, handlerAdded, handlerRemoved, isSharableclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitexceptionCaught, handlerAdded, handlerRemovedprotected void encode(ChannelHandlerContext ctx, MessageNano msg, java.util.List<java.lang.Object> out) throws java.lang.Exception
MessageToMessageEncoder
encode在课程
MessageToMessageEncoder<MessageNano>
ctx - MessageToMessageEncoder属于的ChannelHandlerContext
msg - 要编码到另一个的消息
out - 应该添加编码味精的
List需要做某种聚合
java.lang.Exception - 发生错误时抛出
Copyright © 2008–2018 The Netty Project. All rights reserved.