|
14 | 14 |
|
15 | 15 | /** |
16 | 16 | * Created by yangyibo on 16/12/29. |
| 17 | + * |
17 | 18 | */ |
18 | 19 | @Controller |
19 | 20 | public class WebSocketController { |
20 | | - @MessageMapping("/welcome") |
21 | | - @SendTo("/topic/getResponse") |
| 21 | + |
| 22 | + @MessageMapping("/welcome")//浏览器发送请求通过@messageMapping 映射/welcome 这个地址。 |
| 23 | + @SendTo("/topic/getResponse")//服务器端有消息时,会订阅@SendTo 中的路径的浏览器发送消息。 |
22 | 24 | public Response say(Message message) throws Exception { |
23 | | - Thread.sleep(3000); |
| 25 | + Thread.sleep(1000); |
24 | 26 | return new Response("Welcome, " + message.getName() + "!"); |
25 | 27 | } |
26 | 28 |
|
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 | +// } |
42 | 44 | } |
0 commit comments