Skip to content

Commit 21feef5

Browse files
committed
feat: stomp websocket实例补全
1 parent 4737e94 commit 21feef5

File tree

9 files changed

+10359
-11
lines changed

9 files changed

+10359
-11
lines changed

pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
<groupId>com.alibaba</groupId>
4040
<artifactId>fastjson</artifactId>
4141
</dependency>
42+
<dependency>
43+
<groupId>org.projectlombok</groupId>
44+
<artifactId>lombok</artifactId>
45+
</dependency>
4246
<dependency>
4347
<groupId>org.springframework.boot</groupId>
4448
<artifactId>spring-boot-starter</artifactId>

spring-boot/203-websocket/src/main/java/com/git/hui/boot/websocket/Application.java

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public void sc1() throws IOException {
3434
String rspMsg = Thread.currentThread().getName() + " 自动返回 | sc1:" + LocalDateTime.now();
3535
// MyWebSocketHandler.groupSend();
3636

37+
// 后端主动给前端发送消息
3738
simpMessagingTemplate.convertAndSend("/topic/hello", rspMsg);
3839
}
3940

spring-boot/203-websocket/src/main/java/com/git/hui/boot/websocket/stomp/StompConfiguration.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,29 @@
1313
@EnableWebSocketMessageBroker
1414
public class StompConfiguration implements WebSocketMessageBrokerConfigurer {
1515

16+
/**
17+
* 这里定义的是客户端接收服务端消息的相关信息
18+
*
19+
* @param registry
20+
*/
1621
@Override
1722
public void configureMessageBroker(MessageBrokerRegistry registry) {
18-
// 消息代理指定了客户端订阅地址,以及发送消息的路由地址
23+
// 消息代理指定了客户端订阅地址,前端订阅的就是这个路径, 接收后端发送的消息
24+
// 对应 index.js中的 stompClient.subscribe('/topic/hello'
1925
registry.enableSimpleBroker("/topic");
26+
27+
// 表示配置一个或多个前缀,通过这些前缀过滤出需要被注解方法处理的消息。
28+
// 例如,前缀为 /app 的 destination 可以通过@MessageMapping注解的方法处理,
29+
// 而其他 destination (例如 /topic /queue)将被直接交给 broker 处理
2030
registry.setApplicationDestinationPrefixes("/app");
2131
}
2232

33+
/**
34+
* 添加一个服务端点,来接收客户端的连接
35+
* 即客户端创建ws时,指定的地址, let socket = new WebSocket("ws://ws/hello");
36+
*
37+
* @param registry
38+
*/
2339
@Override
2440
public void registerStompEndpoints(StompEndpointRegistry registry) {
2541
// Endpoint指定了客户端建立连接时的请求地址

spring-boot/203-websocket/src/main/resources/static/css/bootstrap.min.css

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spring-boot/203-websocket/src/main/resources/static/index.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ function setConnected(connected) {
1212
}
1313

1414
function connect() {
15-
var socket = new SockJS('/ws/hello');
15+
// var socket = new SockJS($("#endpoint").val());
16+
var socket = new SockJS("/ws/hello");
1617
stompClient = Stomp.over(socket);
1718
stompClient.connect({}, function (frame) {
1819
setConnected(true);
@@ -34,12 +35,12 @@ function disconnect() {
3435
}
3536

3637
function sendName() {
37-
// 表示讲消息转发到那个目标,类似与http请求中的path路径
38+
// 表示将消息转发到哪个目标,类似与http请求中的path路径,对应的是后端 @MessageMapping 修饰的方法
3839
stompClient.send("/app/hello", {}, JSON.stringify({'name': $("#name").val()}));
3940
}
4041

4142
function showGreeting(message) {
42-
$("#greetings").append("<tr><td>" + message + "</td></tr>");
43+
$("#greetings").prepend("<tr><td>" + message + "</td></tr>");
4344
}
4445

4546
$(function () {

0 commit comments

Comments
 (0)