liusuyi
2025-02-21 8c5435101924d68588448a4c981417df4b4dab1d
ard-work/src/main/java/com/ard/tianyu/websocket/utils/WebSocketUtil.java
@@ -27,7 +27,6 @@
    // 存储 websocket session
    public static final ConcurrentMap<String, WebSocketSession> ONLINE_USER_SESSIONS = new ConcurrentHashMap<>();
    /**
     * @param session 用户 session
     * @param message 发送内容
@@ -50,6 +49,11 @@
        }
    }
    public static void sendMessage(String userId, String message) {
        WebSocketSession session = ONLINE_USER_SESSIONS.get(userId);
        sendMessage(session, message);
    }
    /**
     * @param session 用户 session
     * @param message 发送内容
@@ -58,7 +62,7 @@
        if (session == null) {
            return;
        }
       final InetSocketAddress remoteAddress = session.getRemoteAddress();
        final InetSocketAddress remoteAddress = session.getRemoteAddress();
        if (remoteAddress == null) {
            return;
        }
@@ -106,6 +110,7 @@
        JSONObject jsonObject = new JSONObject(message);
        ONLINE_USER_SESSIONS.forEach((sessionId, session) -> sendMessage(session, jsonObject.toString()));
    }
    /**
     * 推送消息到其他客户端
     *
@@ -115,6 +120,7 @@
        String jsonMessage = JSONObject.toJSONString(message);  // 确保对象转换为 JSON 字符串
        ONLINE_USER_SESSIONS.forEach((sessionId, session) -> sendMessage(session, jsonMessage));
    }
    /**
     * 推送消息到其他客户端
     *
@@ -123,12 +129,9 @@
    public static void sendMessageAll(List message) {
        ONLINE_USER_SESSIONS.forEach((sessionId, session) -> sendMessage(session, JSON.toJSONString(message)));
    }
    /**
     * @Author 刘苏义
     * @Description  发送消息给userId_前缀的人
     * @Date   2024/7/16 10:24
     * @Param
     * @return
     * 发送消息给userId_前缀的人
     */
    public static void sendMessagePrefix(String targetId, String message) {
        String regex = "^" + Pattern.quote(targetId) + "_\\d+$";