liusuyi
2025-02-21 8c5435101924d68588448a4c981417df4b4dab1d
ard-work/src/main/java/com/ard/tianyu/websocket/utils/WebSocketUtil.java
@@ -27,9 +27,6 @@
    // 存储 websocket session
    public static final ConcurrentMap<String, WebSocketSession> ONLINE_USER_SESSIONS = new ConcurrentHashMap<>();
    // 存储 拉流 websocket session
    public static final ConcurrentMap<String, WebSocketSession> STREAM_ONLINE_USER_SESSIONS = new ConcurrentHashMap<>();
    /**
     * @param session 用户 session
     * @param message 发送内容
@@ -52,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 发送内容
@@ -60,7 +62,7 @@
        if (session == null) {
            return;
        }
       final InetSocketAddress remoteAddress = session.getRemoteAddress();
        final InetSocketAddress remoteAddress = session.getRemoteAddress();
        if (remoteAddress == null) {
            return;
        }
@@ -98,6 +100,7 @@
    public static void sendMessageAll(String message) {
        ONLINE_USER_SESSIONS.forEach((sessionId, session) -> sendMessage(session, message));
    }
    /**
     * 推送消息到其他客户端
     *
@@ -107,6 +110,7 @@
        JSONObject jsonObject = new JSONObject(message);
        ONLINE_USER_SESSIONS.forEach((sessionId, session) -> sendMessage(session, jsonObject.toString()));
    }
    /**
     * 推送消息到其他客户端
     *
@@ -116,6 +120,7 @@
        String jsonMessage = JSONObject.toJSONString(message);  // 确保对象转换为 JSON 字符串
        ONLINE_USER_SESSIONS.forEach((sessionId, session) -> sendMessage(session, jsonMessage));
    }
    /**
     * 推送消息到其他客户端
     *
@@ -124,6 +129,7 @@
    public static void sendMessageAll(List message) {
        ONLINE_USER_SESSIONS.forEach((sessionId, session) -> sendMessage(session, JSON.toJSONString(message)));
    }
    /**
     * 发送消息给userId_前缀的人
     */