【发布时间】:2020-10-02 16:37:08
【问题描述】:
是否可以在没有任何分隔符和消息长度知识的情况下使用 netty? 我有以下 ByteToMessageDecoder 实现:
@RequiredArgsConstructor
public class MessageDecoder extends ByteToMessageDecoder {
private static final String ENCODING = "ascii";
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List out) throws UnsupportedEncodingException {
if (!byteBuf.isReadable()) {
return;
}
byte[] bytes = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(bytes);
String outString = new String(bytes, ENCODING);
System.out.println(outString);
out.add(outString);
}
}
问题是 byteBuf 在读取时可能不包含整个消息。因此,消息可能会被拆分为多个消息。 有办法处理吗?
【问题讨论】:
-
您好,如果我的回答对您有帮助,请采纳 :)。如果您自己弄清楚了,请发布您自己的答案。编程愉快!
-
我认为你的问题可以使用
ReplayingDecoderlink来解决