增加websocket日志

This commit is contained in:
1378012178@qq.com 2026-01-13 10:31:41 +08:00
parent 96c3db96ec
commit aed7687a91
1 changed files with 25 additions and 10 deletions

View File

@ -34,14 +34,16 @@ public class SdWebsocket {
SdWebsocket.sysUserAPI = sysUserAPI; SdWebsocket.sysUserAPI = sysUserAPI;
} }
/**线程安全Map*/ /**
* 线程安全Map
*/
private static ConcurrentHashMap<String, Session> sessionPool = new ConcurrentHashMap<>(); private static ConcurrentHashMap<String, Session> sessionPool = new ConcurrentHashMap<>();
@OnOpen @OnOpen
public void onOpen(Session session, @PathParam(value = "userId") String userId) { public void onOpen(Session session, @PathParam(value = "userId") String userId) {
try { try {
sessionPool.put(userId, session); sessionPool.put(userId, session);
log.info("【系统 SdWebsocket】有新的连接总数为:"+sessionPool.size()); log.info("【系统 SdWebsocket】有新的连接总数为:" + sessionPool.size());
broadcastUserList(); broadcastUserList();
} catch (Exception e) { } catch (Exception e) {
@ -52,7 +54,7 @@ public class SdWebsocket {
public void onClose(@PathParam("userId") String userId) { public void onClose(@PathParam("userId") String userId) {
try { try {
sessionPool.remove(userId); sessionPool.remove(userId);
log.info("【系统 SdWebsocket】连接断开总数为:"+sessionPool.size()); log.info("【系统 SdWebsocket】连接断开总数为:" + sessionPool.size());
broadcastUserList(); broadcastUserList();
} catch (Exception e) { } catch (Exception e) {
@ -65,16 +67,16 @@ public class SdWebsocket {
*/ */
@OnMessage @OnMessage
public void onMessage(String message, @PathParam(value = "userId") String userId) { public void onMessage(String message, @PathParam(value = "userId") String userId) {
if(!"ping".equals(message) && !WebsocketConst.CMD_CHECK.equals(message)){ if (!"ping".equals(message) && !WebsocketConst.CMD_CHECK.equals(message)) {
log.info("【系统 SdWebsocket】收到客户端消息:"+message); log.info("【系统 SdWebsocket】收到客户端消息:" + message);
}else{ } else {
log.info("【系统 SdWebsocket】收到客户端消息:"+message); log.info("【系统 SdWebsocket】收到客户端消息:" + message);
JSONObject json; JSONObject json;
try { try {
json = JSON.parseObject(message); json = JSON.parseObject(message);
this.sendMessage(userId, "ping"); this.sendMessage(userId, "ping");
} catch (Exception e) { } catch (Exception e) {
log.warn("【SdWebsocket】收到不合法的消息:"+message); log.warn("【SdWebsocket】收到不合法的消息:" + message);
return; return;
} }
} }
@ -98,7 +100,7 @@ public class SdWebsocket {
* @param message * @param message
*/ */
public void sendMessage(String message) { public void sendMessage(String message) {
log.info("【系统 SdWebsocket】广播消息:"+message); log.info("【系统 SdWebsocket】广播消息:" + message);
for (Session session : sessionPool.values()) { for (Session session : sessionPool.values()) {
session.getAsyncRemote().sendText(message); session.getAsyncRemote().sendText(message);
} }
@ -125,6 +127,19 @@ public class SdWebsocket {
log.info("【系统 SdWebsocket】当前在线用户: [{}] 共 {} 人", userList.toString(), sessionPool.size()); log.info("【系统 SdWebsocket】当前在线用户: [{}] 共 {} 人", userList.toString(), sessionPool.size());
Session session = sessionPool.get(userId); Session session = sessionPool.get(userId);
if (session != null) {
log.info("【系统 SdWebsocket】获取到用户 {} 的 session: id={}, isOpen={}, maxIdleTimeout={}ms,requestURI={}, queryString={}",
userId,
session.getId(),
session.isOpen(),
session.getMaxIdleTimeout(),
session.getRequestURI(),
session.getQueryString());
} else {
log.info("【系统 SdWebsocket】未找到用户 {} 的 sessionsessionPool 中不存在该用户", userId);
}
if (session != null && session.isOpen()) { if (session != null && session.isOpen()) {
session.getAsyncRemote().sendText(message); session.getAsyncRemote().sendText(message);
log.debug("【系统 SdWebsocket】消息发送成功"); log.debug("【系统 SdWebsocket】消息发送成功");
@ -150,7 +165,7 @@ public class SdWebsocket {
// 构建消息type固定为userListusers是当前所有用户ID // 构建消息type固定为userListusers是当前所有用户ID
JSONObject msg = new JSONObject(); JSONObject msg = new JSONObject();
msg.put("type", "userList"); msg.put("type", "userList");
msg.put("users",sysUserAPI.queryByEmployees(String.join(",", sessionPool.keySet()))); msg.put("users", sysUserAPI.queryByEmployees(String.join(",", sessionPool.keySet())));
msg.put("timestamp", System.currentTimeMillis()); msg.put("timestamp", System.currentTimeMillis());
sendMessage(msg.toJSONString()); sendMessage(msg.toJSONString());