企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
~~~ public class WebsocketChannelInitalizer extends ChannelInitializer<SocketChannel> { protected void initChannel(SocketChannel socketChannel) throws Exception { ChannelPipeline pipeline = socketChannel.pipeline(); // websocket基于http协议,所以要有http编解码器 pipeline.addLast(new HttpServerCodec()); // 对写大数据流的支持 pipeline.addLast(new ChunkedWriteHandler()); // POST请求时,对应的参数信息是保存在message body中的,如果只是单纯的用HttpServerCodec是无法完全的解析HttpPOST请求的, // 因为HttpServerCodec只能获取uri中参数,所以需要加上HttpObjectAggregator // 对httpMessage进行聚合,聚合成FullHttpRequest或FullHttpResponse // 几乎在netty中的编程,都会用到此handler pipeline.addLast(new HttpObjectAggregator(8192));// 聚合 // ws : 指定给客户端连接访问的路由 // 帮你处理一些繁重的复杂的事情,例如handshaking(ping, pong, close) // 对于websocket来讲,都是以frames进行传输的,不同的数据类型对应的frames也不同 pipeline.addLast(new WebSocketServerProtocolHandler("/ws")); // pipeline.addLast(new TextWebsocketFrameHandler()); } } ~~~