mitao
2024-06-21 fb2128a3bf8d5a6f1e91ba735241af5924e74ffe
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
package com.ruoyi.system.websocket;
 
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import javax.websocket.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
/**
 * websocket 客户端用户集
 * 
 * @author ruoyi
 */
public class WebSocketUsers
{
    private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketUsers.class);
 
    private static Map<String, Session> USERS = new ConcurrentHashMap<>();
    private static Map<String, Integer> USER_TYPES = new ConcurrentHashMap<>();
 
    public static void put(String key, Session session, Integer clientType)
    {
        USERS.put(key, session);
        USER_TYPES.put(key, clientType);
    }
 
    public static boolean remove(Session session)
    {
        String key = null;
        boolean flag = USERS.containsValue(session);
        if (flag)
        {
            Set<Map.Entry<String, Session>> entries = USERS.entrySet();
            for (Map.Entry<String, Session> entry : entries)
            {
                Session value = entry.getValue();
                if (value.equals(session))
                {
                    key = entry.getKey();
                    break;
                }
            }
        }
        else
        {
            return true;
        }
        return remove(key);
    }
 
    public static boolean remove(String key)
    {
        LOGGER.info("\n 正在移出用户 - {}", key);
        Session remove = USERS.remove(key);
        USER_TYPES.remove(key);
        if (remove != null)
        {
            boolean containsValue = USERS.containsValue(remove);
            LOGGER.info("\n 移出结果 - {}", containsValue ? "失败" : "成功");
            return containsValue;
        }
        else
        {
            return true;
        }
    }
 
    public static Map<String, Session> getUsers()
    {
        return USERS;
    }
 
    public static Integer getUserType(String key) {
        return USER_TYPES.get(key);
    }
 
    public static void sendMessageToUsersByText(String message)
    {
        Collection<Session> values = USERS.values();
        for (Session value : values)
        {
            sendMessageToUserByText(value, message);
        }
    }
 
    public static void sendMessageToUserByText(Session session, String message)
    {
        if (session != null)
        {
            try
            {
                session.getBasicRemote().sendText(message);
            }
            catch (IOException e)
            {
                LOGGER.error("\n[发送消息异常]", e);
            }
        }
        else
        {
            LOGGER.info("\n[你已离线]");
        }
    }
 
    /**
     * 根据客户端类型发送消息
     *
     * @param clientType 1=会员小程序 2=拍卖师小程序
     * @param message    发送的消息
     */
    public static void sendMessageToUsersByType(Integer clientType, String message) {
        for (Map.Entry<String, Session> entry : USERS.entrySet()) {
            String key = entry.getKey();
            Session session = entry.getValue();
            if (clientType.equals(USER_TYPES.get(key))) {
                sendMessageToUserByText(session, message);
            }
        }
    }
}