Netty以4096字节切断收到的WebSocket消息

时间:2018-03-19 18:10:58

标签: websocket client netty

我在netty中有一个WebSocket客户端,我从服务器(~90kb)收到一个大框架。当使用OkHttp执行此操作时,一切正常,在接收netty(4.1.11.Final)时,无论我的选择如何,消息总是会被切断为4096.

以下是设置客户端的代码:

SslContext sslCtx = SslContextBuilder.forClient().build();

final WebSocketClientHandler handler = getWebSocketClientHandler(WebSocketClientHandshakerFactory.newHandshaker(
                    uri, WebSocketVersion.V13, null, true, new DefaultHttpHeaders()),
                    this::messageHandler);

Bootstrap b = new Bootstrap();
b.group(eventLoopGroup)
                    .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, Math.toIntExact(connectionTimeout.toMillis()))
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) {
                            ChannelPipeline p = ch.pipeline();
                            p.addLast(sslCtx.newHandler(ch.alloc(), host, port));

                            WebSocketClientExtensionHandler clientExtensionHandler = getWebSocketClientExtensionHandler();
                            List<ChannelHandler> handlers = new ArrayList<>(4);
                            handlers.add(new HttpClientCodec(500_000, 504_000, 504_000, false, false, 504_000));
                            handlers.add(new HttpObjectAggregator(500_000));
                            handlers.add(handler);
                            handlers.add(WebSocketClientCompressionHandler.INSTANCE);
                            p.addLast(handlers.toArray(new ChannelHandler[handlers.size()]));
                        }
                    });

我忘记了一些明显的事情吗?

1 个答案:

答案 0 :(得分:0)

您的WebSocketClientHandler位于管道中的压缩处理程序之前。如果服务器向您发送压缩消息,您可能会在处理程序中看到较小的帧大小(与原始的未压缩消息相比)。将压缩处理程序放在WebSocketClientHandler之前。