forked from 527515025/springBoot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebSocketController.java
More file actions
87 lines (76 loc) · 3.04 KB
/
Copy pathWebSocketController.java
File metadata and controls
87 lines (76 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.us.example.controller;
import com.us.example.bean.Message;
import com.us.example.bean.Response;
import com.us.example.service.WebSocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.security.Principal;
/**
* Created by yangyibo on 16/12/29.
*
*/
@CrossOrigin
@Controller
public class WebSocketController {
@Autowired
private WebSocketService ws;
@Autowired
private SimpMessagingTemplate messagingTemplate;
@RequestMapping(value = "/login")
public String login(){
return "login";
}
@RequestMapping(value = "/ws")
public String ws(){
return "ws";
}
@RequestMapping(value = "/chat")
public String chat(){
return "chat";
}
//http://localhost:8080/ws
@MessageMapping("/welcome")//浏览器发送请求通过@messageMapping 映射/welcome 这个地址。
@SendTo("/topic/getResponse")//服务器端有消息时,会订阅@SendTo 中的路径的浏览器发送消息。
public Response say(Message message) throws Exception {
Thread.sleep(1000);
return new Response("Welcome, " + message.getName() + "!");
}
//http://localhost:8080/Welcome1
@RequestMapping("/Welcome1")
@ResponseBody
public String say2()throws Exception
{
ws.sendMessage();
return "is ok";
}
@MessageMapping("/chat")
//在springmvc 中可以直接获得principal,principal 中包含当前用户的信息
public void handleChat(Principal principal, Message message) {
/**
* 此处是一段硬编码。如果发送人是wyf 则发送给 wisely 如果发送人是wisely 就发送给 wyf。
* 通过当前用户,然后查找消息,如果查找到未读消息,则发送给当前用户。
*/
if (principal.getName().equals("admin")) {
//通过convertAndSendToUser 向用户发送信息,
// 第一个参数是接收消息的用户,第二个参数是浏览器订阅的地址,第三个参数是消息本身
messagingTemplate.convertAndSendToUser("abel",
"/queue/notifications", principal.getName() + "-send:"
+ message.getName());
/**
* 72 行操作相等于
* messagingTemplate.convertAndSend("/user/abel/queue/notifications",principal.getName() + "-send:"
+ message.getName());
*/
} else {
messagingTemplate.convertAndSendToUser("admin",
"/queue/notifications", principal.getName() + "-send:"
+ message.getName());
}
}
}