网站首页 > 博客文章 正文
WebSocket是一种运行在TCP协议上的全双工网络通信协议,尤其在客户端和服务端需要进行实时通信的场景中使用非常广泛。
在传统的HTTP协议中,主要是基于请求响应模式,也就是说客户端发起请求,然后服务端收到请求之后,响应对应的内容。而对于WebSocket来讲它允许的是服务端和客户端建立一个全双工的连接连接建立之后,可以实现互相之间的消息发送,不需要每次都发送请求重新建立连接。这时的WebSocket非常适合在一些需要低延迟和高频率消息交换的应用场景中,比如常见的在线聊天、实时通知、游戏等。
在Spring Boot中,可以通过集成Netty实现WebSocket通信。Netty是一个高性能的网络框架,提供了对WebSocket协议的良好支持。以下是使用Spring Boot和Netty实现WebSocket通信的步骤。
创建Spring Boot项目并引入依赖
首先创建一个Spring Boot项目,并在pom.xml文件中添加所需的依赖
<dependencies>
<!-- Spring Boot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Netty -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!-- Netty WebSocket 依赖 -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.72.Final</version>
</dependency>
</dependencies>
这里我们spring-boot-starter-webflux 默认使用 Netty 作为底层服务器,可以很方便地结合WebSocket实现实时通信。
配置WebSocket Server
想要实现WebSocket,首先需要定义个WebSocketServerInitializer初始化类,通过这个初始化类来初始化基于Netty的WebSocket服务。如下所示。
public class WebSocketServerInitializer extends ChannelInitializer<SocketChannel> {
private final SslContext sslContext;
public WebSocketServerInitializer(SslContext sslContext) {
this.sslContext = sslContext;
}
@Override
protected void initChannel(SocketChannel ch) {
var pipeline = ch.pipeline();
// SSL handler
if (sslContext != null) {
pipeline.addLast(sslContext.newHandler(ch.alloc()));
}
// HTTP handler
pipeline.addLast(new HttpServerCodec());
pipeline.addLast(new HttpObjectAggregator(65536));
pipeline.addLast(new ChunkedWriteHandler());
// WebSocket handler
pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));
// Custom handler to process messages
pipeline.addLast(new TextWebSocketFrameHandler());
}
}
在这个类中配置了HTTP 和 WebSocket 的解码器与聚合器,然后通过一个自定义的消息处理器TextWebSocketFrameHandler来处理WebSocket消息。
实现WebSocket消息处理器
在TextWebSocketFrameHandler实现消息的逻辑处理负责接收WebSocket的文本消息,如下所示。
public class TextWebSocketFrameHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) {
// 打印接收到的消息
System.out.println("Received message: " + msg.text());
// 向客户端发送响应消息
ctx.channel().writeAndFlush(new TextWebSocketFrame("Server received your message: " + msg.text()));
}
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
System.out.println("Client connected: " + ctx.channel().id().asLongText());
}
@Override
public void handlerRemoved(ChannelHandlerContext ctx) {
System.out.println("Client disconnected: " + ctx.channel().id().asLongText());
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
}
channelRead0 方法用于处理接收到的 WebSocket 消息,并发送响应。handlerAdded 和 handlerRemoved 方法分别处理客户端的连接和断开事件。
配置WebSocket服务器启动类
创建WebSocketServer类来启动WebSocket服务,如下所示,在Spring Boot启动时初始化,并在应用关闭时关闭WebSocket服务。
@Component
public class WebSocketServer {
private final EventLoopGroup bossGroup = new NioEventLoopGroup();
private final EventLoopGroup workerGroup = new NioEventLoopGroup();
private Channel channel;
@PostConstruct
public void start() {
var sslContext = (SslContext) null; // 可根据需要配置 SSL
try {
var serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new WebSocketServerInitializer(sslContext));
var channelFuture = serverBootstrap.bind(8080).sync();
channel = channelFuture.channel();
System.out.println("WebSocket server started on port 8080");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@PreDestroy
public void stop() {
if (channel != null) {
channel.close();
}
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
总结
通过以上步骤,在Spring Boot中成功集成了Netty实现WebSocket通信。我们可以启动Spring Boot应用,然后尝试访问 ws://localhost:8080/ws,就可以进行WebSocket连接和消息通信。可以使用浏览器或其他WebSocket客户端工具(如 Postman 或 websocat)连接服务器,进行测试。
- 上一篇: Postman替代品,这两款开源工具值得尝试
- 下一篇: 乖乖 Apipost竟然这么香!是我发现晚了
猜你喜欢
- 2025-01-06 2023 年春招 100 道软件测试面试题,附答案!
- 2025-01-06 完整的企业微信 API 接口调用教程:快速调用
- 2025-01-06 工具推荐Apifox——API 文档、API 调试、API Mock、API 自动化测试
- 2025-01-06 FastAPI 鉴权解析:实现身份验证与权限控制的关键步骤
- 2025-01-06 Spring Boot 常用注解大全:每个程序员必备
- 2025-01-06 2023年值得推荐的 API 开发工具
- 2025-01-06 一招搞定外部请求,这款 HTTP 客户端框架真的很强大!
- 2025-01-06 处理 Spring Boot Actuator 未授权访问的技巧与方法
- 2025-01-06 「Python系列」python的Flask项目实战(一)
- 2025-01-06 GitHub 新项目!快速生成接口文档和模拟数据,开发联调效率杠杠滴
你 发表评论:
欢迎- 368℃用AI Agent治理微服务的复杂性问题|QCon
- 368℃手把手教程「JavaWeb」优雅的SpringMvc+Mybatis整合之路
- 359℃初次使用IntelliJ IDEA新建Maven项目
- 352℃Maven技术方案最全手册(mavena)
- 348℃安利Touch Bar 专属应用,让闲置的Touch Bar活跃起来!
- 347℃InfoQ 2024 年趋势报告:架构篇(infoq+2024+年趋势报告:架构篇分析)
- 345℃IntelliJ IDEA 2018版本和2022版本创建 Maven 项目对比
- 343℃从头搭建 IntelliJ IDEA 环境(intellij idea建包)
- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)