Netty的Channel.write写数据不能超过1024byte的问题解决

博客讲述使用Netty建立WebSocket服务端时遇到的问题,服务端能正常接收消息,但调用writeAndFlush后客户端无法收到长度超1024byte的消息。经调试发现问题,检查依赖发现版本不一致,通过在pom.xml添加代码片段解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开发的时候总会遇到一些有趣的问题,Netty发送不了1024byte!已解决!

问题描述

这次主要使用netty建立WebSocket服务端,服务端可以正常收到消息,服务端调用writeAndFlush
客户端无法收到消息

调试

将io.netty日志等级降低到debug
日志打印 Encoding WebSocket Frame opCode=1 length=1074
length超过1024客户端就无法收到消息

服务端收到:{"random":"5926","code":1,"roomId":1}
2021-01-09 00:00:50.214 DEBUG 20316 --- [ntLoopGroup-3-1] i.n.h.c.h.w.WebSocket08FrameEncoder      : Encoding WebSocket Frame opCode=1 length=1074
检查依赖

发现io.netty版本不一致,可能会导致冲突!
在这里插入图片描述

解决

pom.xml添加以下代码片段

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>io.netty</groupId>
				<artifactId>netty-bom</artifactId>
				<version>4.1.36.Final</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
结果

在这里插入图片描述
搞定!关闭浏览器
在这里插入图片描述

代码片段1
@Component
@Slf4j
public class WebSocketNettyServer {

    private EventLoopGroup parentGroup = new NioEventLoopGroup();
    private EventLoopGroup childGroup = new NioEventLoopGroup();

    private Channel channel;

    public  ChannelFuture bing(InetSocketAddress address) {
        ChannelFuture channelFuture = null;

        try {

            ServerBootstrap b = new ServerBootstrap()
                    .group(parentGroup, childGroup)
                    .channel(NioServerSocketChannel.class)    //非阻塞模式
                    //子处理器,用于处理wss
                    .childHandler(new HttpChannelInitializer())
                    .option(ChannelOption.SO_BACKLOG, 1024)
                    // 两小时没有数据通信时,TCP自动发送一个活动探测报文
                    .childOption(ChannelOption.SO_KEEPALIVE, true);
            channelFuture = b.bind(address).syncUninterruptibly();
            channel = channelFuture.channel();
        } catch (Exception e) {
            log.error(e.getMessage());
        } finally {
            if (null != channelFuture && channelFuture.isSuccess()) {
                log.info("dnetty server start done. ");
            } else {
                log.error("netty server start error. ");
            }
        }
        return channelFuture;
    }

    public  void destroy() {
        if (null == channel) return;
        channel.close();
        parentGroup.shutdownGracefully();
        childGroup.shutdownGracefully();
    }

    public Channel getChannel() {
        return channel;
    }

}
代码片段2
public class HttpChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
		// HTTP 服务的解码器
        pipeline.addLast("http-codec", new HttpServerCodec());
        pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
        pipeline.addLast("http-chunked", new ChunkedWriteHandler());

        pipeline.addLast(new MyChannelHandler());
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值