|
1
|
package com.huaheng.pc.stompwebsocket.controller;
|
|
2
|
|
|
3
|
import com.huaheng.pc.stompwebsocket.WebsocketConstants;
|
|
4
|
import com.huaheng.pc.stompwebsocket.domain.Message;
|
|
5
6
|
import com.huaheng.pc.stompwebsocket.domain.StompPayload;
import com.huaheng.pc.stompwebsocket.service.StompWebSocketServiceImpl;
|
|
7
|
import org.springframework.context.annotation.Lazy;
|
|
8
9
10
|
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
|
|
11
|
import org.springframework.util.LinkedMultiValueMap;
|
|
12
13
14
15
16
|
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
|
|
17
18
|
import javax.annotation.Resource;
|
|
19
|
@Lazy
|
|
20
21
|
@Controller
public class WebSocketController {
|
|
22
23
|
@Resource
private StompWebSocketServiceImpl stompWebSocketService;
|
|
24
|
|
|
25
26
|
@GetMapping("/monitor/websocket")
public String manage(){
|
|
27
|
return "monitor/websocket/manage";
|
|
28
29
|
}
|
|
30
|
|
|
31
32
33
34
35
36
37
|
/**
* 服务器接收websocket上行的普通文本消息,再通过websocket广播出去
*/
@MessageMapping("/websocket/message/string")
@SendTo(WebsocketConstants.TOPIC_MESSAGE)
public String handleStringMsgByWebsocket(String msg){
return msg;
|
|
38
39
|
}
|
|
40
41
42
|
/**
* 服务器接收websocket上行的json消息,再通过websocket发送出去
*/
|
|
43
|
@MessageMapping("/websocket/message/json")
|
|
44
|
public void handleJsonMsgByWebsocket(@RequestBody StompPayload<Message> payload){
|
|
45
|
stompWebSocketService.send(WebsocketConstants.TOPIC_MESSAGE, payload);
|
|
46
47
|
}
|
|
48
|
/**
|
|
49
50
51
|
* 服务器接收confirm消息
*/
@MessageMapping("/websocket/message/confirm")
|
|
52
|
public void handleConfirmMsgByWebsocket(org.springframework.messaging.Message headers){
|
|
53
54
55
56
57
58
59
|
try{
String msg_id = ((LinkedMultiValueMap<String, String>) headers.getHeaders().get("nativeHeaders")).get("msg-id").get(0);
stompWebSocketService.confirmMsg(msg_id);
}catch (Exception e){
}
|
|
60
61
62
|
}
/**
|
|
63
64
65
66
|
* 服务器接收httpget上行的普通文本消息,再通过websocket广播出去
* http://127.0.0.1:8888/wms/websocket/message/string?msg=huaheng_robot
*/
@GetMapping("/websocket/message/string")
|
|
67
|
@ResponseBody
|
|
68
|
public String handleStringMsgByHttpGet(String msg){
|
|
69
|
stompWebSocketService.sendMessageBroadcast(WebsocketConstants.TOPIC_MESSAGE, msg);
|
|
70
71
72
|
return "ok";
}
|
|
73
74
75
76
77
78
|
/**
* 服务器接收httppost上行的json消息,再通过websocket广播出去
* StompPayload中封装目标用户和消息体
* http://127.0.0.1:8888/wms/websocket/message/json
*/
@PostMapping("/websocket/message/json")
|
|
79
|
@ResponseBody
|
|
80
|
public String handleJsonMsgByHttpPost(@RequestBody StompPayload<Object> payload){
|
|
81
|
stompWebSocketService.send(WebsocketConstants.TOPIC_MESSAGE, payload);
|
|
82
83
|
return "ok";
}
|
|
84
85
|
|
|
86
|
}
|