上节课我们讲解了STOMP的基本原理和在Spring Boot中的处理流程。本篇文章以具体的实例带大家了解如何在Spring Boot中使用STOMP协议。
实例场景
用户访问页面,进入“技术交流群”页面,自动登录会话,可发送消息、离开等操作。同时,服务器端会定时推送“计数”到前端。
实战代码
引入依赖
在Spring Boot项目中引入以下依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<