guohongjin
2024-05-15 5b7639f0bd9e056738ec15100ed0532e965c6cd5
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
package cn.stylefeng.roses.kernel.socket.business.websocket.session;
 
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.socket.api.session.pojo.SocketSession;
import cn.stylefeng.roses.kernel.socket.business.websocket.operator.channel.GunsSocketOperator;
 
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
 
/**
 * 会话中心
 * <p>
 * 维护所有的会话
 *
 * @author majianguo
 * @date 2021/6/1 下午1:43
 */
public class SessionCenter {
 
    /**
     * 所有用户会话维护
     */
    private static ConcurrentMap<String, List<SocketSession<GunsSocketOperator>>> socketSessionMap = new ConcurrentHashMap<>();
 
    /**
     * 获取维护的所有会话
     *
     * @return {@link ConcurrentMap< String, SocketSession<GunsSocketOperator>>}
     * @author majianguo
     * @date 2021/6/1 下午2:13
     **/
    public static ConcurrentMap<String, List<SocketSession<GunsSocketOperator>>> getSocketSessionMap() {
        return socketSessionMap;
    }
 
    /**
     * 根据用户ID获取会话信息列表
     *
     * @param userId 用户ID
     * @return {@link SocketSession <GunsSocketOperator>}
     * @author majianguo
     * @date 2021/6/1 下午1:48
     **/
    public static List<SocketSession<GunsSocketOperator>> getSessionByUserId(String userId) {
        return socketSessionMap.get(userId);
    }
 
    /**
     * 根据用户ID和消息类型获取会话信息列表
     *
     * @param userId 用户ID
     * @return {@link SocketSession <GunsSocketOperator>}
     * @author majianguo
     * @date 2021/6/1 下午1:48
     **/
    public static List<SocketSession<GunsSocketOperator>> getSessionByUserIdAndMsgType(String userId) {
        return socketSessionMap.get(userId);
    }
 
    /**
     * 根据会话ID获取会话信息
     *
     * @param sessionId 会话ID
     * @return {@link SocketSession <GunsSocketOperator>}
     * @author majianguo
     * @date 2021/6/1 下午1:48
     **/
    public static SocketSession<GunsSocketOperator> getSessionBySessionId(String sessionId) {
        Set<Map.Entry<String, List<SocketSession<GunsSocketOperator>>>> entrySet = socketSessionMap.entrySet();
        for (Map.Entry<String, List<SocketSession<GunsSocketOperator>>> stringListEntry : entrySet) {
            List<SocketSession<GunsSocketOperator>> stringListEntryValue = stringListEntry.getValue();
            for (SocketSession<GunsSocketOperator> socketSession : stringListEntryValue) {
                if (sessionId.equals(socketSession.getSessionId())) {
                    return socketSession;
                }
            }
        }
        return null;
    }
 
    /**
     * 设置会话
     *
     * @param socketSession 会话详情
     * @author majianguo
     * @date 2021/6/1 下午1:49
     **/
    public static void addSocketSession(SocketSession<GunsSocketOperator> socketSession) {
        List<SocketSession<GunsSocketOperator>> socketSessions = socketSessionMap.get(socketSession.getUserId());
        if (ObjectUtil.isEmpty(socketSessions)) {
            socketSessions = Collections.synchronizedList(new ArrayList<>());
            socketSessionMap.put(socketSession.getUserId(), socketSessions);
        }
        socketSessions.add(socketSession);
    }
 
    /**
     * 连接关闭
     *
     * @param sessionId 会话ID
     * @author majianguo
     * @date 2021/6/1 下午3:25
     **/
    public static void closed(String sessionId) {
        // 删除维护关系
        SocketSession<GunsSocketOperator> operatorSocketSession = getSessionBySessionId(sessionId);
        if (ObjectUtil.isNotEmpty(operatorSocketSession)) {
            operatorSocketSession.getSocketOperatorApi().close();
        }
    }
 
    /**
     * 删除维护关系
     *
     * @param sessionId 会话ID
     * @return {@link cn.stylefeng.roses.kernel.socket.api.session.pojo.SocketSession<cn.stylefeng.roses.kernel.socket.business.websocket.operator.channel.GunsSocketOperator>}
     * @author majianguo
     * @date 2021/8/30 9:20
     **/
    public static SocketSession<GunsSocketOperator> deleteById(String sessionId) {
        // 获取所有人员的连接会话信息
        Set<Map.Entry<String, List<SocketSession<GunsSocketOperator>>>> entrySet = socketSessionMap.entrySet();
        Iterator<Map.Entry<String, List<SocketSession<GunsSocketOperator>>>> iterator = entrySet.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, List<SocketSession<GunsSocketOperator>>> next = iterator.next();
            List<SocketSession<GunsSocketOperator>> value = next.getValue();
            if (ObjectUtil.isNotEmpty(value)) {
 
                // 找出该人员的指定会话信息
                Iterator<SocketSession<GunsSocketOperator>> socketSessionIterator = value.iterator();
                while (socketSessionIterator.hasNext()) {
                    SocketSession<GunsSocketOperator> operatorSocketSession = socketSessionIterator.next();
                    if (operatorSocketSession.getSessionId().equals(sessionId)) {
                        // 在列表中删除
                        socketSessionIterator.remove();
                        return operatorSocketSession;
                    }
                }
            }
        }
 
        return null;
    }
}