提交 | 用户 | 时间
|
0ca254
|
1 |
package com.jcdm.framework.websocket; |
A |
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
import java.util.Collection; |
|
5 |
import java.util.Map; |
|
6 |
import java.util.Set; |
|
7 |
import java.util.concurrent.ConcurrentHashMap; |
|
8 |
import javax.websocket.Session; |
|
9 |
import org.slf4j.Logger; |
|
10 |
import org.slf4j.LoggerFactory; |
|
11 |
|
|
12 |
/** |
|
13 |
* websocket 客户端用户集 |
|
14 |
* |
|
15 |
* @author ruoyi |
|
16 |
*/ |
|
17 |
public class WebSocketUsers |
|
18 |
{ |
|
19 |
/** |
|
20 |
* WebSocketUsers 日志控制器 |
|
21 |
*/ |
|
22 |
private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketUsers.class); |
|
23 |
|
|
24 |
/** |
|
25 |
* 用户集 |
|
26 |
*/ |
|
27 |
private static Map<String, Session> USERS = new ConcurrentHashMap<String, Session>(); |
|
28 |
|
|
29 |
/** |
|
30 |
* 存储用户 |
|
31 |
* |
|
32 |
* @param key 唯一键 |
|
33 |
* @param session 用户信息 |
|
34 |
*/ |
|
35 |
public static void put(String key, Session session) |
|
36 |
{ |
|
37 |
USERS.put(key, session); |
|
38 |
} |
|
39 |
|
|
40 |
/** |
|
41 |
* 移除用户 |
|
42 |
* |
|
43 |
* @param session 用户信息 |
|
44 |
* |
|
45 |
* @return 移除结果 |
|
46 |
*/ |
|
47 |
public static boolean remove(Session session) |
|
48 |
{ |
|
49 |
String key = null; |
|
50 |
boolean flag = USERS.containsValue(session); |
|
51 |
if (flag) |
|
52 |
{ |
|
53 |
Set<Map.Entry<String, Session>> entries = USERS.entrySet(); |
|
54 |
for (Map.Entry<String, Session> entry : entries) |
|
55 |
{ |
|
56 |
Session value = entry.getValue(); |
|
57 |
if (value.equals(session)) |
|
58 |
{ |
|
59 |
key = entry.getKey(); |
|
60 |
break; |
|
61 |
} |
|
62 |
} |
|
63 |
} |
|
64 |
else |
|
65 |
{ |
|
66 |
return true; |
|
67 |
} |
|
68 |
return remove(key); |
|
69 |
} |
|
70 |
|
|
71 |
/** |
|
72 |
* 移出用户 |
|
73 |
* |
|
74 |
* @param key 键 |
|
75 |
*/ |
|
76 |
public static boolean remove(String key) |
|
77 |
{ |
|
78 |
LOGGER.info("\n 正在移出用户 - {}", key); |
|
79 |
Session remove = USERS.remove(key); |
|
80 |
if (remove != null) |
|
81 |
{ |
|
82 |
boolean containsValue = USERS.containsValue(remove); |
|
83 |
LOGGER.info("\n 移出结果 - {}", containsValue ? "失败" : "成功"); |
|
84 |
return containsValue; |
|
85 |
} |
|
86 |
else |
|
87 |
{ |
|
88 |
return true; |
|
89 |
} |
|
90 |
} |
|
91 |
|
|
92 |
/** |
|
93 |
* 获取在线用户列表 |
|
94 |
* |
|
95 |
* @return 返回用户集合 |
|
96 |
*/ |
|
97 |
public static Map<String, Session> getUsers() |
|
98 |
{ |
|
99 |
return USERS; |
|
100 |
} |
|
101 |
|
|
102 |
/** |
|
103 |
* 群发消息文本消息 |
|
104 |
* |
|
105 |
* @param message 消息内容 |
|
106 |
*/ |
|
107 |
public static void sendMessageToUsersByText(String message) |
|
108 |
{ |
|
109 |
Collection<Session> values = USERS.values(); |
|
110 |
for (Session value : values) |
|
111 |
{ |
|
112 |
sendMessageToUserByText(value, message); |
|
113 |
} |
|
114 |
} |
|
115 |
|
|
116 |
/** |
|
117 |
* 发送文本消息 |
|
118 |
* |
6a53e6
|
119 |
* @param session 自己的用户名 |
0ca254
|
120 |
* @param message 消息内容 |
A |
121 |
*/ |
|
122 |
public static void sendMessageToUserByText(Session session, String message) |
|
123 |
{ |
|
124 |
if (session != null) |
|
125 |
{ |
|
126 |
try |
|
127 |
{ |
|
128 |
session.getBasicRemote().sendText(message); |
|
129 |
} |
|
130 |
catch (IOException e) |
|
131 |
{ |
|
132 |
LOGGER.error("\n[发送消息异常]", e); |
|
133 |
} |
|
134 |
} |
|
135 |
else |
|
136 |
{ |
|
137 |
LOGGER.info("\n[你已离线]"); |
|
138 |
} |
|
139 |
} |
|
140 |
} |