提交 | 用户 | 时间
|
e57a89
|
1 |
package com.jcdm.framework.websocket; |
懒 |
2 |
|
|
3 |
import java.util.concurrent.Semaphore; |
|
4 |
import javax.websocket.OnClose; |
|
5 |
import javax.websocket.OnError; |
|
6 |
import javax.websocket.OnMessage; |
|
7 |
import javax.websocket.OnOpen; |
|
8 |
import javax.websocket.Session; |
|
9 |
import javax.websocket.server.PathParam; |
|
10 |
import javax.websocket.server.ServerEndpoint; |
|
11 |
import org.slf4j.Logger; |
|
12 |
import org.slf4j.LoggerFactory; |
|
13 |
import org.springframework.stereotype.Component; |
|
14 |
|
|
15 |
/** |
|
16 |
* websocket 消息处理 |
|
17 |
* |
|
18 |
* @author ruoyi |
|
19 |
*/ |
|
20 |
@Component |
|
21 |
@ServerEndpoint("/websocket/message/{userId}") |
|
22 |
public class WebSocketServer |
|
23 |
{ |
|
24 |
/** |
|
25 |
* WebSocketServer 日志控制器 |
|
26 |
*/ |
|
27 |
private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketServer.class); |
|
28 |
|
|
29 |
/** |
|
30 |
* 默认最多允许同时在线人数100 |
|
31 |
*/ |
|
32 |
public static int socketMaxOnlineCount = 100; |
|
33 |
|
|
34 |
private static Semaphore socketSemaphore = new Semaphore(socketMaxOnlineCount); |
|
35 |
|
|
36 |
/**接收userId*/ |
|
37 |
private String userId=""; |
|
38 |
|
|
39 |
/** |
|
40 |
* 连接建立成功调用的方法 |
|
41 |
*/ |
|
42 |
@OnOpen |
|
43 |
public void onOpen(Session session, @PathParam("userId") String userId) throws Exception |
|
44 |
{ |
|
45 |
boolean semaphoreFlag = false; |
|
46 |
// 尝试获取信号量 |
|
47 |
semaphoreFlag = SemaphoreUtils.tryAcquire(socketSemaphore); |
|
48 |
if (!semaphoreFlag) |
|
49 |
{ |
|
50 |
// 未获取到信号量 |
|
51 |
LOGGER.error("\n 当前在线人数超过限制数- {}", socketMaxOnlineCount); |
|
52 |
WebSocketUsers.sendMessageToUserByText(session, "当前在线人数超过限制数:" + socketMaxOnlineCount); |
|
53 |
session.close(); |
|
54 |
} |
|
55 |
else |
|
56 |
{ |
|
57 |
// 添加用户 |
|
58 |
// WebSocketUsers.put(session.getId(), session); |
|
59 |
WebSocketUsers.put(userId, session); |
|
60 |
LOGGER.info("\n 建立连接 - {}", session); |
|
61 |
LOGGER.info("\n 当前人数 - {}", WebSocketUsers.getUsers().size()); |
|
62 |
WebSocketUsers.sendMessageToUserByText(session, "连接成功"); |
|
63 |
} |
|
64 |
} |
|
65 |
|
|
66 |
/** |
|
67 |
* 连接关闭时处理 |
|
68 |
*/ |
|
69 |
@OnClose |
|
70 |
public void onClose(Session session) |
|
71 |
{ |
|
72 |
LOGGER.info("\n 关闭连接 - {}", session); |
|
73 |
// 移除用户 |
|
74 |
WebSocketUsers.remove(session.getId()); |
|
75 |
// 获取到信号量则需释放 |
|
76 |
SemaphoreUtils.release(socketSemaphore); |
|
77 |
} |
|
78 |
|
|
79 |
/** |
|
80 |
* 抛出异常时处理 |
|
81 |
*/ |
|
82 |
@OnError |
|
83 |
public void onError(Session session, Throwable exception) throws Exception |
|
84 |
{ |
|
85 |
if (session.isOpen()) |
|
86 |
{ |
|
87 |
// 关闭连接 |
|
88 |
session.close(); |
|
89 |
} |
|
90 |
String sessionId = session.getId(); |
|
91 |
LOGGER.info("\n 连接异常 - {}", sessionId); |
|
92 |
LOGGER.info("\n 异常信息 - {}", exception); |
|
93 |
// 移出用户 |
|
94 |
WebSocketUsers.remove(sessionId); |
|
95 |
// 获取到信号量则需释放 |
|
96 |
SemaphoreUtils.release(socketSemaphore); |
|
97 |
} |
|
98 |
|
|
99 |
/** |
|
100 |
* 服务器接收到客户端消息时调用的方法 |
|
101 |
*/ |
|
102 |
@OnMessage |
|
103 |
public void onMessage(String message, Session session) |
|
104 |
{ |
|
105 |
String msg = message.replace("你", "我").replace("吗", ""); |
|
106 |
// WebSocketUsers.sendMessageToUserByText(session, msg); |
|
107 |
} |
|
108 |
} |