shikeyin
2024-01-11 65da8373531677b1c37a98f53eaa30c892f35e5a
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package com.iplatform.tcp.controller;
 
import com.iplatform.base.SystemController;
import com.iplatform.tcp.util.ws.WebBroadCastResponse;
import com.walker.infrastructure.utils.DateUtils;
import com.walker.infrastructure.utils.NumberGenerator;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.push.Notification;
import com.walker.push.NotificationChannel;
import com.walker.push.PushManager;
import com.walker.push.PushResult;
import com.walker.tcp.ConnectionManager;
import com.walker.tcp.connect.LongConnection;
import com.walker.tcp.lb.LongConnectionMeta;
import com.walker.tcp.lb.RedisConnectionMetaCache;
import com.walker.tcp.lb.RedisConnectionNameCache;
import com.walker.tcp.netty.WebSocketEngine;
import com.walker.web.ResponseValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@RestController
@RequestMapping("/test/tcp")
public class TestTcpController extends SystemController {
 
    private WebSocketEngine webSocketEngine;
    private PushManager pushManager;
    private RedisConnectionMetaCache connectionMetaCache;
    private RedisConnectionNameCache connectionNameCache;
 
    @Autowired
    public TestTcpController(WebSocketEngine webSocketEngine, PushManager pushManager
            , @Nullable RedisConnectionMetaCache connectionMetaCache
            , @Nullable RedisConnectionNameCache connectionNameCache){
        this.webSocketEngine = webSocketEngine;
        this.pushManager = pushManager;
        this.connectionMetaCache = connectionMetaCache;
        this.connectionNameCache = connectionNameCache;
    }
 
    /**
     * 获取websocket基本情况。
     * @return
     * @date 2023-12-14
     */
    @RequestMapping("/ws/info")
    public ResponseValue getWebsocketLinkInfo(){
        Map<String, Object> infoMap = new HashMap<>(8);
        if(this.connectionNameCache != null){
            long totalWebsocketSize = this.connectionNameCache.getCache().getPersistentSize();
            infoMap.put("total_websocket", totalWebsocketSize);
            infoMap.put("create_time", connectionNameCache.getCreateTime());
            infoMap.put("cache_name", this.connectionNameCache.getProviderName());
        }
        if(this.connectionMetaCache != null){
            infoMap.put("connection_meta_size", this.connectionMetaCache.getCache().getPersistentSize());
        }
        return ResponseValue.success(infoMap);
    }
 
    /**
     * 获取一个链接信息。
     * @param userId 连接绑定的用户名称(name),一般为用户ID。
     * @return
     * @date 2023-12-14
     */
    @RequestMapping("/ws/connection")
    public ResponseValue getWebsocketConnection(String userId){
        if(StringUtils.isEmpty(userId)){
            return ResponseValue.error("userId is required!");
        }
        Map<String, Object> infoMap = new HashMap<>(8);
 
        ConnectionManager connectionManager = this.webSocketEngine.getConnectionManager();
        if(connectionManager != null){
            LongConnection connection = (LongConnection) connectionManager.getConnectionByName(userId);
            if(connection == null){
                return ResponseValue.success("连接不存在:" + userId);
            }
 
            if(connection instanceof LongConnectionMeta){
                infoMap.put("type", "集群连接对象(LongConnectionMeta),不是当前主机物理连接");
            }
            infoMap.put("id", connection.getId());
            infoMap.put("host", connection.getConnectionHost());
            infoMap.put("name", connection.getName());
            infoMap.put("alreadyLogin", connection.getAlreadyLogin());
            infoMap.put("engineId", connection.getEngineId());
            infoMap.put("lastTime", connection.getLastTime());
        } else {
            infoMap.put("error", "connectionManager 为空");
        }
        return ResponseValue.success(infoMap);
    }
 
    @RequestMapping("/ws/send")
    public ResponseValue sendTestCommand(String name, String uid){
        WebBroadCastResponse msg = new WebBroadCastResponse();
        msg.setMessageId(String.valueOf(NumberGenerator.getSequenceNumber()));
        msg.setName(uid);
        msg.setData(name + ": " + msg.getMessageId());
        webSocketEngine.sendBroadcast(msg);
        return ResponseValue.success();
    }
 
    @RequestMapping("/push")
    public ResponseValue pushOne(String type, String userId){
        PushResult pushResult = null;
        if(type.equals("1")){
            logger.info("推送短信测试,user = 0");
            pushResult = this.pushManager.push(this.acquireNotification(NotificationChannel.Sms, "13838277463"), null);
        } else if(type.equals("2")){
            if(StringUtils.isEmpty(userId)){
                userId = "0";
            }
            pushResult = this.pushManager.push(this.acquireNotification(NotificationChannel.WebSocket, userId), null);
        } else {
            throw new UnsupportedOperationException("暂不支持其他推送方式:" + type);
        }
        return ResponseValue.success(pushResult);
    }
 
    private Notification acquireNotification(NotificationChannel channel, String user){
        Notification n = new Notification();
        n.setId(NumberGenerator.generatorHexUUID());
        n.setCreateTime(DateUtils.getDateTimeNumber());
        n.setCreator("creator");
        n.setFrom("shikeying");
        n.setContent("这是一个测试");
 
        List<NotificationChannel> channels = new ArrayList<>(2);
        channels.add(channel);
        n.setChannelList(channels);
        n.setPersistent(true);
        n.setTitle("title");
 
        List<String> userList = new ArrayList<>(2);
        userList.add(user);
        n.setReceiverList(userList);
        return n;
    }
}