Blame view

src/main/java/com/huaheng/pc/stompwebsocket/controller/WebSocketController.java 2.97 KB
1
package com.huaheng.pc.stompwebsocket.controller;
周峰 authored
2
3
import com.huaheng.pc.stompwebsocket.WebsocketConstants;
周峰 authored
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;
周峰 authored
8
9
10
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
周峰 authored
11
import org.springframework.util.LinkedMultiValueMap;
周峰 authored
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
周峰 authored
20
21
@Controller
public class WebSocketController {
22
23
    @Resource
    private StompWebSocketServiceImpl stompWebSocketService;
周峰 authored
24
25
26
    @GetMapping("/monitor/websocket")
    public String manage(){
27
        return "monitor/websocket/manage";
28
29
    }
周峰 authored
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;
周峰 authored
38
39
    }
40
41
42
    /**
     * 服务器接收websocket上行的json消息,再通过websocket发送出去
     */
周峰 authored
43
    @MessageMapping("/websocket/message/json")
周峰 authored
44
    public void handleJsonMsgByWebsocket(@RequestBody StompPayload<Message> payload){
45
        stompWebSocketService.send(WebsocketConstants.TOPIC_MESSAGE, payload);
周峰 authored
46
47
    }
48
    /**
周峰 authored
49
50
51
     * 服务器接收confirm消息
     */
    @MessageMapping("/websocket/message/confirm")
周峰 authored
52
    public void handleConfirmMsgByWebsocket(org.springframework.messaging.Message headers){
周峰 authored
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){

        }
周峰 authored
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")
周峰 authored
67
    @ResponseBody
68
    public String handleStringMsgByHttpGet(String msg){
69
        stompWebSocketService.sendMessageBroadcast(WebsocketConstants.TOPIC_MESSAGE, msg);
周峰 authored
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")
周峰 authored
79
    @ResponseBody
80
    public String handleJsonMsgByHttpPost(@RequestBody StompPayload<Object> payload){
81
        stompWebSocketService.send(WebsocketConstants.TOPIC_MESSAGE, payload);
周峰 authored
82
83
        return "ok";
    }
84
85
周峰 authored
86
}