我将框架 Netty 与 Android 一起使用,当我收到大量数据时遇到问题:
@Override public void channelRead(final ChannelHandlerContext ctx, Object msg) {
bufferIn = (ByteBuf) msg;
if (bufferIn.readableBytes() >= 4) {
size = bufferIn.readInt();
result = bufferIn.toString(CharsetUtil.ISO_8859_1);
if(size == result.length){
Log.d("D", "RECEIVED: " + size + " " + result);
}
ctx.flush();
}
bufferIn.release();
}
当我收到数据包中的数据时,我不知道如何接收这些包裹。谢谢
我添加以下行:
ch.pipeline().addLast(new NettyDecoder(), new NettyHandler(service, size, message, listener));
在解码器上:
class NettyDecoder extends ByteToMessageDecoder
@Override protected void decode(final ChannelHandlerContext theContext, final ByteBuf in, List<Object> out) throws Exception {
if (in.readableBytes() < 4) {
return;
}
in.markReaderIndex();
int dataLength = in.readInt();
if (in.readableBytes() < dataLength) {
in.resetReaderIndex();
return;
}
byte[] decoded = new byte[dataLength];
in.readBytes(decoded);
out.add(decoded);
//out.add(in.readBytes(in.readableBytes()));
}
在处理程序上:
void channelRead(final ChannelHandlerContext ctx, Object msg) {
ByteBuf m = (ByteBuf) msg;
bufferIn.writeBytes(m);
m.release();
if (bufferIn.readableBytes() >= 4) {
size = bufferIn.readInt();
for (int i = 0; i < bufferIn.capacity(); i ++) {
byte b = bufferIn.getByte(i);
result += (char) b;
}
}
Log.d("WEB", "RESULT: " + size + " " + result.getBytes().length + " " + result);
bufferIn.release();
但是现在我在 NettyHandler 上没有收到任何消息。
有人可以教我如何接收处理程序上的消息吗?