Skip to content

Commit 4751474

Browse files
committed
web socket
1 parent c0f74d9 commit 4751474

10 files changed

Lines changed: 9077 additions & 29 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
# IDEA ignore
1717
.idea
1818
.class
19-
19+
*.class
2020

springWebSocket/src/main/java/com/us/example/bean/Message.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/**
44
* Created by yangyibo on 16/12/29.
5+
* 浏览器向服务器发送的消息使用此类接受
56
*/
67
public class Message {
78
private String name;

springWebSocket/src/main/java/com/us/example/bean/Response.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33
/**
44
* Created by yangyibo on 16/12/29.
5+
* 服务器向浏览器发送的此类消息。
56
*/
67
public class Response {
8+
public void setResponseMessage(String responseMessage) {
9+
this.responseMessage = responseMessage;
10+
}
11+
712
private String responseMessage;
813
public Response(String responseMessage){
914
this.responseMessage = responseMessage;
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.us.example.config;
22

3-
import org.springframework.context.annotation.Configuration;
43
import org.springframework.context.annotation.Configuration;
54
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
65
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
@@ -12,19 +11,22 @@
1211
*/
1312
@Configuration
1413
@EnableWebSocketMessageBroker
14+
//通过EnableWebSocketMessageBroker 开启使用STOMP协议来传输基于代理(message broker)的消息,此时浏览器支持使用@MessageMapping 就像支持@RequestMapping一样。
1515
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer{
1616

1717

1818
@Override
1919
public void registerStompEndpoints(StompEndpointRegistry registry) { //endPoint 注册协议节点,并映射指定的URl
2020

21-
registry.addEndpoint("/endpointWisely").withSockJS();
22-
registry.addEndpoint("/endpointChat").withSockJS();
21+
registry.addEndpoint("/endpointWisely").withSockJS();//注册一个Stomp 协议的endpoint,并指定 SockJS协议。
22+
// registry.addEndpoint("/endpointChat").withSockJS();
2323
}
2424

2525

2626
@Override
27-
public void configureMessageBroker(MessageBrokerRegistry registry) {
28-
registry.enableSimpleBroker("/queue","/topic"); //2
27+
public void configureMessageBroker(MessageBrokerRegistry registry) {//配置消息代理(message broker)
28+
registry.enableSimpleBroker("/topic"); //广播式应配置一个/topic 消息代理
29+
// registry.enableSimpleBroker("/queue","/topic"); //2
30+
2931
}
3032
}

springWebSocket/src/main/java/com/us/example/controller/WebSocketController.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,31 @@
1414

1515
/**
1616
* Created by yangyibo on 16/12/29.
17+
*
1718
*/
1819
@Controller
1920
public class WebSocketController {
20-
@MessageMapping("/welcome")
21-
@SendTo("/topic/getResponse")
21+
22+
@MessageMapping("/welcome")//浏览器发送请求通过@messageMapping 映射/welcome 这个地址。
23+
@SendTo("/topic/getResponse")//服务器端有消息时,会订阅@SendTo 中的路径的浏览器发送消息。
2224
public Response say(Message message) throws Exception {
23-
Thread.sleep(3000);
25+
Thread.sleep(1000);
2426
return new Response("Welcome, " + message.getName() + "!");
2527
}
2628

27-
@Autowired
28-
private SimpMessagingTemplate messagingTemplate;//1
29-
30-
@MessageMapping("/chat")
31-
public void handleChat(Principal principal, String msg) { //2
32-
if (principal.getName().equals("wyf")) {//3
33-
messagingTemplate.convertAndSendToUser("wisely",
34-
"/queue/notifications", principal.getName() + "-send:"
35-
+ msg);
36-
} else {
37-
messagingTemplate.convertAndSendToUser("wyf",
38-
"/queue/notifications", principal.getName() + "-send:"
39-
+ msg);
40-
}
41-
}
29+
// @Autowired
30+
// private SimpMessagingTemplate messagingTemplate;//1
31+
//
32+
// @MessageMapping("/chat")
33+
// public void handleChat(Principal principal, String msg) { //2
34+
// if (principal.getName().equals("wyf")) {//3
35+
// messagingTemplate.convertAndSendToUser("wisely",
36+
// "/queue/notifications", principal.getName() + "-send:"
37+
// + msg);
38+
// } else {
39+
// messagingTemplate.convertAndSendToUser("wyf",
40+
// "/queue/notifications", principal.getName() + "-send:"
41+
// + msg);
42+
// }
43+
// }
4244
}

springWebSocket/src/main/resources/templates/ws.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@
3232
}
3333

3434
function connect() {
35-
var socket = new SockJS('/endpointWisely'); //1
36-
stompClient = Stomp.over(socket);
37-
stompClient.connect({}, function(frame) {
35+
var socket = new SockJS('/endpointWisely'); //链接SockJS 的endpoint 名称为"/endpointWisely"
36+
stompClient = Stomp.over(socket);//使用stomp子协议的WebSocket 客户端
37+
stompClient.connect({}, function(frame) {//链接Web Socket的服务端。
3838
setConnected(true);
3939
console.log('Connected: ' + frame);
40-
stompClient.subscribe('/topic/getResponse', function(respnose){ //2
40+
stompClient.subscribe('/topic/getResponse', function(respnose){ //订阅/topic/getResponse 目标发送的消息。这个是在控制器的@SendTo中定义的。
4141
showResponse(JSON.parse(respnose.body).responseMessage);
4242
});
4343
});
@@ -54,7 +54,7 @@
5454

5555
function sendName() {
5656
var name = $('#name').val();
57-
//3
57+
//通过stompClient.send 向/welcome 目标 发送消息,这个是在控制器的@messageMapping 中定义的。
5858
stompClient.send("/welcome", {}, JSON.stringify({ 'name': name }));
5959
}
6060

0 commit comments

Comments
 (0)