mitao
2025-02-14 ce0403c1d94ba031ecc832d0acfcb3650c33ef6b
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
package com.ruoyi.system.controller;
 
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.system.api.domain.WebsocketMessageDTO;
import com.ruoyi.system.websocket.WebSocketUsers;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
@RequestMapping("/websocket")
public class WebSocketController {
 
    @GetMapping("/push")
    public R<?> push() {
        WebSocketUsers.sendMessageToUsersByText("长江长江,我是黄河!");
        return R.ok();
    }
 
    @GetMapping("/push/{type}/{msg}")
    public R<?> push(@PathVariable("type") Integer type, @PathVariable("msg") String msg) {
        WebSocketUsers.sendMessageToUsersByType(type, msg);
        return R.ok();
    }
 
    @InnerAuth
    @PostMapping("/push-by-client-type")
    public R<?> pushByClientType(@RequestBody WebsocketMessageDTO dto) {
        WebSocketUsers.sendMessageToUsersByType(dto.getClientType().getCode(), dto.getMessage());
        return R.ok();
    }
 
    @InnerAuth
    @GetMapping("/push-all/{message}")
    public R<?> pushAll(@PathVariable("message") String message) {
        WebSocketUsers.sendMessageToUsersByText(message);
        return R.ok();
    }
 
    /**
     * 批量向指定用户推送消息
     * @param dto
     * @return
     */
    @InnerAuth
    @PostMapping("/push-batch")
    public R<?> pushBatch(@RequestBody WebsocketMessageDTO dto) {
        WebSocketUsers.sendMessageToUserById(dto.getUserId(), dto.getMessage());
        return R.ok();
    }
    /**
     * 通过用户id推送消息
     * @param type
     * @param msg
     * @return
     */
    @GetMapping("/push-by-user-id/{userId}/{msg}")
    public R<?> pushByUserId(@PathVariable("userId") Long userId,
            @PathVariable("msg") String msg) {
        WebSocketUsers.sendMessageToUserById(userId, msg);
        return R.ok();
    }
}