This commit is contained in:
xd 2024-05-20 11:08:48 +08:00
parent 1a0d0bac93
commit 22a44adc04
1 changed files with 15 additions and 4 deletions

View File

@ -30,9 +30,13 @@ public class WebsocketEndpointImpl implements WebsocketEndpoint {
*/ */
private static Map<String, Map<String, WebSocketBean>> webSocketInfo; private static Map<String, Map<String, WebSocketBean>> webSocketInfo;
private static Map<String,WebSocketBean> concurrentHashMap;
static { static {
// concurrent包的线程安全map // concurrent包的线程安全map
webSocketInfo = new ConcurrentHashMap<String, Map<String, WebSocketBean>>(); webSocketInfo = new ConcurrentHashMap<String, Map<String, WebSocketBean>>();
concurrentHashMap = new ConcurrentHashMap();
} }
@ -40,19 +44,26 @@ public class WebsocketEndpointImpl implements WebsocketEndpoint {
public void onOpen(Session session, EndpointConfig config, @PathParam("userId") String userId,@PathParam("projectId") String projectId) { public void onOpen(Session session, EndpointConfig config, @PathParam("userId") String userId,@PathParam("projectId") String projectId) {
WebSocketBean bean = new WebSocketBean(); WebSocketBean bean = new WebSocketBean();
bean.setSession(session); bean.setSession(session);
/*
Map<String,WebSocketBean> concurrentHashMap = new ConcurrentHashMap(); Map<String,WebSocketBean> concurrentHashMap = new ConcurrentHashMap();
*/
concurrentHashMap.put(userId,bean); concurrentHashMap.put(userId,bean);
webSocketInfo.put(projectId, concurrentHashMap); webSocketInfo.put(projectId, concurrentHashMap);
log.info("ws项目:"+projectId+",客户端连接服务器userId :" + userId + "当前连接数:" + countUser(projectId)); for (String userid:concurrentHashMap.keySet()){
log.info("ws项目:"+projectId+",客户端现连接userId :" + userid);
}
log.info("当前连接数:" + countUser(projectId));
} }
@OnClose @OnClose
public void onClose(Session session, @PathParam("userId") String userId,@PathParam("projectId") String projectId) { public void onClose(Session session, @PathParam("userId") String userId,@PathParam("projectId") String projectId) {
// 客户端断开连接移除websocket对象 // 客户端断开连接移除websocket对象
Map<String,WebSocketBean> concurrentHashMap = webSocketInfo.get(projectId); Map<String,WebSocketBean> concurrentHashMap = webSocketInfo.get(projectId);
if(concurrentHashMap != null){concurrentHashMap.remove(userId);} if(concurrentHashMap != null){
log.info("ws项目:"+projectId+",客户端断开连接,当前连接数:" + countUser(projectId)); concurrentHashMap.remove(userId);
log.info("ws项目:"+projectId+",客户端断开userId :" + userId);
}
log.info("当前连接数:" + countUser(projectId));
} }
@OnMessage @OnMessage