WebSocket - Netty服务端构建
一、WebSocket二、基于Netty构建WebSocket服务端
2.1 入门案例2.2 WebSocket相关的Netty内置处理类2.3 SpringBoot整合Netty方案 万事如意,阖家安康
WebSocket - Netty服务端构建| 文章目录 |
|---|
| 在线websocket测试-online tool-postjson (coolaf.com) |
| WebSocket协议深入探究 - 云+社区 - 腾讯云 (tencent.com) |
| / |
初步总结几句话
- 是单个TCP连接上的全双工通信协议服务端与客户端之间仅需一次握手,即可创建持久性连接进行双向数据传输
WebSocket和Socket的区别
Socket:客户端主动请求,服务器被动应答,单向数据传输;WebSocket:全双工模式,仅需一次握手;
实际上,WebSocket使用的场景在于实时要求高的场景。
二、基于Netty构建WebSocket服务端对于客户端的WebSocket不作演示,只学服务端
2.1 入门案例NettyServer基本配置
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
public class NettyServer {
public void ServerStart() throws InterruptedException {
// 连接请求处理
EventLoopGroup bossGroup = new NioEventLoopGroup(3);
// IO请求处理
EventLoopGroup workerGroup = new NioEventLoopGroup(5);
// 基本配置
ServerBootstrap serverBootstrap =
new ServerBootstrap().group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
// 处理器管道配置
serverBootstrap.childHandler(new ChannelInitializer() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
System.out.println("此时客户端进来了:" + "客户 SocketChannel hashcode=" + ch.hashCode());
ChannelPipeline pipeline = ch.pipeline();
// 请求/响应的编解码器
pipeline.addLast(new HttpServerCodec());
// 将多消息转换为单一请求/响应对象,解码成FullHttpRequest
// maxContentLength – 聚合内容的最大长度(以字节为单位)
pipeline.addLast(new HttpObjectAggregator(65535));
// WebSocket协议处理
pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));
// 下面才是自定义的WebSocket业务处理器
pipeline.addLast(new WebSocketDemoHandler());
}
});
// 服务器端口配置及监听
ChannelFuture channelFuture = serverBootstrap.bind(16668).sync();
channelFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (channelFuture.isSuccess()) {
System.out.println("监听端口 16668 成功");
} else {
System.out.println("监听端口 16668 失败");
}
}
});
// 关闭通道并监听
channelFuture.channel().closeFuture().sync();
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
WebSocketDemoHandler业务处理器
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.codec.http.websocketx.TextWebSocketframe; import io.netty.handler.codec.http.websocketx.WebSocketframe; public class WebSocketDemoHandler extends SimpleChannelInboundHandler{ @Override protected void channelRead0(ChannelHandlerContext ctx, WebSocketframe msg) throws Exception { if (msg instanceof TextWebSocketframe) { // 消息接收 String requestStr = ((TextWebSocketframe) msg).text(); System.out.println("我接受到的消息是:" + requestStr); // 消息回复 TextWebSocketframe textWebSocketframe = new TextWebSocketframe("echo"); ctx.channel().writeAndFlush(textWebSocketframe); } else { System.out.println("这是一个非文本消息 不做处理"); } } }
最后找一个在线WebSocket测试的网站查看效果
2.2 WebSocket相关的Netty内置处理类在上文的案例中,我使用了TextWebSocketframe去解析文本数据,其实还有其他数据帧格式
| 名称 | 描述 |
|---|---|
| BinaryWebSocketframe | 二进制数据的WebSocketframe数据帧 |
| TextWebSocketframe | 文本数据的WebSocketframe数据帧 |
| CloseWebSocketframe | 控制帧,代表一个结束请求,包含结束的状态和结束原因 |
| ContinuationWebSocketframe | 当发送的内容多一个数据帧时,消息就会拆分为多个WebSocketframe数据帧发送,这个类型的数据帧专用来发送剩下的内容。ContinuationWebSocketframe可以用来发送后续的文本或者二进制数据帧 |
| PingWebSocketframe | 控制帧,对应协议报文的操作码为0x9,是WebSocket的心跳帧,由服务端发送 |
| PongWebSocketframe | 控制帧,对应协议报文的操作码为0xA,是WebSocket的心跳帧,由客户端发送 |
然而,在管道Pipeline上的处理器,也有相应的一些内置处理类
| 名称 | 描述 |
|---|---|
| WebSocketServerProtocolHandler | 协议升级时的处理(握手处理),另外此处理器还负责对WebSocket的三个控制帧ClosePingPong进行处理 |
| WebSocketServerProtocolHandshakeHandler | 协议升级时的处理(握手处理),握手完成后(连接建立),这个处理器会触发HANDSHAKE_COMPLETE的用户事件,表示握手成功 |
| WebSocketframeEncoder | 编码器,负责WebSocket数据帧编码 |
| WebSocketframeDecoder | 解码器,负责WebSocket数据帧解码 |
对照着上图,来看看下图的管道装配
算了,看到这里,我只想说…真难啊我丢
2.3 SpringBoot整合Netty方案| 文章目录 |
|---|
| netty-websocket-spring-boot-starter/README_zh.md at master · YeautyYE GitHub |
你学废了吗
万事如意,阖家安康


