@ChannelHandler.Sharable public class StringEncoder extends MessageToMessageEncoder<java.lang.CharSequence>
String编码为ByteBuf 。
TCP / IP套接字中基于文本的行协议的典型设置为:
ChannelPipeline pipeline = ...;
// Decoders
pipeline.addLast("frameDecoder", new LineBasedFrameDecoder(80));
pipeline.addLast("stringDecoder", new StringDecoder(CharsetUtil.UTF_8));
// Encoder
pipeline.addLast("stringEncoder", new StringEncoder(CharsetUtil.UTF_8));
然后您可以使用String而不是ByteBuf作为消息:
void channelRead(ChannelHandlerContext ctx, String msg) {
ch.write("Did you say '" + msg + "'?\n");
}
ChannelHandler.Sharable| Constructor and Description |
|---|
StringEncoder()
用当前系统字符集创建一个新实例。
|
StringEncoder(java.nio.charset.Charset charset)
用指定的字符集创建一个新的实例。
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
encode(ChannelHandlerContext ctx, java.lang.CharSequence 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, handlerRemovedpublic StringEncoder()
public StringEncoder(java.nio.charset.Charset charset)
protected void encode(ChannelHandlerContext ctx, java.lang.CharSequence msg, java.util.List<java.lang.Object> out) throws java.lang.Exception
MessageToMessageEncoder
encode在课堂上
MessageToMessageEncoder<java.lang.CharSequence>
ctx - 这MessageToMessageEncoder所属的ChannelHandlerContext
msg - 要编码到另一个的消息
out - 应该添加编码味精的
List需要做某种聚合
java.lang.Exception - 发生错误时抛出
Copyright © 2008–2018 The Netty Project. All rights reserved.