44323
2023-09-16 a99684a9a4ecbdd71d1c93ef824f7741ff52c5d9
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
package cn.mb.cloud.gateway.controller;
 
 
import cn.mb.cloud.gateway.auth.ResultUtil;
import cn.mb.cloud.gateway.util.applets.NettyWebSocketController;
import cn.mb.cloud.gateway.util.echo.NettyChannelMap;
import cn.mb.cloud.gateway.util.echo.NettyServerController;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.web.bind.annotation.*;
 
import java.util.HashMap;
import java.util.Map;
 
@RestController
@RequestMapping("/netty")
public class NettyController {
 
 
    /**   3.4.5.6
     * 向客户端推送消息
     * @param id
     * @param msg
     */
    @ResponseBody
    @GetMapping("/sendMsgToClient")
    public String sendMsgToClient(String id, Integer type, String msg){
        System.out.println(id+"|"+type+"|"+msg+"!!!!!!!!");
        if(true){//用户端
            String[] split = id.split("_");
            ChannelHandlerContext channel = NettyChannelMap.getData("USER" + split[0]);
            if(null != channel){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("code", 200);
                jsonObject.put("msg", "SUCCESS");
                jsonObject.put("method", "CLASS");
                Map<String, Object> map = new HashMap<>();
                jsonObject.put("data", map);
                NettyServerController.sendMsgToClient(channel, jsonObject.toJSONString());
                return JSON.toJSONString(ResultUtil.success());
            }
            return JSON.toJSONString(ResultUtil.error("推送失败-----用户id=" + id));
 
        }
        return JSON.toJSONString(ResultUtil.error("推送失败"));
    }
}