Netty系列:Springboot+Netty优雅的创建websocket客户端 (附源码下载)
文章目录
- Springboot-cli 开发脚手架系列
- 前言
- 1. 环境
- 2. 引入websocket编码解码器
- 3. 编写websocket处理器
- 4. http测试接口编写
- 5. 效果演示
- 6. 源码分享
前言
首先我们需要使用Netty搭建基础的tcp框架,参考Springboot使用Netty优雅的创建TCP客户端(附源码),接下来我们开始集成websocket。
结尾有完整源码下载地址
- pom.xml
io.netty netty-all ${netty-all.version}
- yml开启日记debug级别打印
# 日记配置
logging:
level:
# 开启debug日记打印
com.netty: debug
2. 引入websocket编码解码器
这里我们需要加入websocket编码解码器,因为websocket的握手是通过http完成的,所以我们还需要加入http的编码器。
@Component @RequiredArgsConstructor public class ChannelInit extends ChannelInitializer3. 编写websocket处理器{ private final MessageHandler messageHandler; @Override protected void initChannel(SocketChannel channel) { channel.pipeline() // 每隔60s的时间触发一次userEventTriggered的方法,并且指定IdleState的状态位是WRITER_IDLE,事件触发给服务器发送ping消息 .addLast("idle", new IdleStateHandler(0, 60, 0, TimeUnit.SECONDS)) // 添加解码器 .addLast(new HttpClientCodec()) .addLast(new ChunkedWriteHandler()) .addLast(new HttpObjectAggregator(1024 * 1024 * 10)) .addLast(new WebSocketFrameAggregator(1024 * 62)) // 添加消息处理器 .addLast("messageHandler", messageHandler); } }
- 修改消息处理器MessageHandler.java
- 增加两个全局变量,保存当前连接
private WebSocketClientHandshaker handShaker;
private ChannelPromise handshakeFuture;
- 连接成功开始握手
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
// 保存Promise
this.handshakeFuture = ctx.newPromise();
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
log.debug("n");
log.debug("握手开始,channelId:{}", ctx.channel().id());
handShaker = WebSocketClientHandshakerFactory.newHandshaker(new URI("ws://" + WebsocketClient.connectedIp + ":" + WebsocketClient.connectedPort + "/ws"), WebSocketVersion.V13, null, false, new DefaultHttpHeaders());
handShaker.handshake(ctx.channel());
super.channelActive(ctx);
}
- 完整代码
@Slf4j @Component @ChannelHandler.Sharable @RequiredArgsConstructor public class MessageHandler extends SimpleChannelInboundHandler4. http测试接口编写
- pom.xml加入web依赖
org.springframework.boot spring-boot-starter-web
- HttpApi .java
@RequiredArgsConstructor
@RestController
@Slf4j
public class HttpApi {
private final WebsocketClient websocketClient;
@GetMapping("/send")
public String send(String message) {
websocketClient.getSocketChannel().writeAndFlush(new TextWebSocketFrame(message));
return "发送成功";
}
@PostMapping("/send/json")
public String send(@RequestBody JSONObject body) {
websocketClient.getSocketChannel().writeAndFlush(new TextWebSocketFrame(body.toJSONString()));
return "发送成功";
}
@GetMapping("connect")
public String connect(String ip, Integer port) throws Exception {
websocketClient.connect(ip, port);
return "重启指令发送成功";
}
@GetMapping("reconnect")
public String reconnect() throws Exception {
websocketClient.reconnect();
return "重启指令发送成功";
}
}
5. 效果演示
-
客户端服务类和启动类基础模块的搭建参考开头提供的连接进行搭建即可,这里就不重复了
-
我们这里通过Springboot+Netty优雅的开发websocket高性能服务器搭建的服务器配合测试
-
测试接口
基础接口 http://localhost:9999
# 1. 发送消息
/send?message=hello
# 2. 连接
/connect?ip=192.168.0.99&port=20000
# 3. 重连
/reconnect
# 5. 发送json
```json
Request URL: http://localhost:9999/send/json
Request Method: POST
Request Headers:
{
"Content-Type":"application/json"
}
Request Body:
{
"msgId": 1,
"type": 1,
"data": {
"message":"hello"
}
}
- 效果
- Springboot-cli开发脚手架,集合各种常用框架使用案例,完善的文档,致力于让开发者快速搭建基础环境并让应用跑起来。
- 项目源码github地址
- 项目源码国内gitee地址



