前言
websockt是需要依赖http1.1 然后返回其响应码101 再开始由http协议转为 webSocket
1、MyServer
/** * @author wzcstart * @date 2021/6/30 - 1:49 */ public class MyServer { public static void main(String[] args) throws Exception{ EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO))//netty自带得日志处理器 .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { //拿到pipeline ChannelPipeline pipeline = ch.pipeline(); //webSocket 协议是 需要依赖 http1.1 协议 //添加http 编解码器 pipeline.addLast(new HttpServerCodec()); //是以块方式写,添加ChunkedWriteHandler处理器 pipeline.addLast(new ChunkedWriteHandler());