package com.ard.work.websocket.utils; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import lombok.extern.slf4j.Slf4j; import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketSession; import java.io.IOException; import java.net.InetSocketAddress; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @ClassName WebSocketUtils * @Description: websocket工具类 * @Author 刘苏义 * @Date 2023/1/27 7:46 * @Version 1.0 */ @Slf4j public final class PTZWebSocketUtils { // 存储 websocket session public static final ConcurrentMap ONLINE_USER_SESSIONS = new ConcurrentHashMap<>(); /** * @param session 用户 session * @param message 发送内容 */ public static void sendMessage(WebSocketSession session, String message) { if (session == null) { return; } final InetSocketAddress remoteAddress = session.getRemoteAddress(); if (remoteAddress == null) { return; } synchronized (session) { try { //log.debug("发送消息:" + message); session.sendMessage(new TextMessage(String.join(", ", message))); } catch (IOException e) { log.error("sendMessage IOException ", e); } } } /** * @param session 用户 session * @param message 发送内容 */ public static void sendMessage(WebSocketSession session, Map message) { if (session == null) { return; } final InetSocketAddress remoteAddress = session.getRemoteAddress(); if (remoteAddress == null) { return; } synchronized (session) { try { session.sendMessage(new TextMessage(JSONObject.toJSONString(message))); } catch (IOException e) { log.error("sendMessage IOException ", e); } } } public static void sendMessage(WebSocketSession session, List message) { if (session == null) { return; } final InetSocketAddress remoteAddress = session.getRemoteAddress(); if (remoteAddress == null) { return; } synchronized (session) { try { session.sendMessage(new TextMessage(JSONObject.toJSONString(message))); } catch (IOException e) { log.error("sendMessage IOException ", e); } } } /** * 推送消息到其他客户端 * * @param message */ public static void sendMessageAll(String message) { ONLINE_USER_SESSIONS.forEach((sessionId, session) -> sendMessage(session, message)); } /** * 推送消息到其他客户端 * * @param message */ public static void sendMessageAll(Map message) { JSONObject jsonObject = new JSONObject(message); ONLINE_USER_SESSIONS.forEach((sessionId, session) -> sendMessage(session, jsonObject.toString())); } /** * 推送消息到其他客户端 * * @param message */ 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 */ public static void sendMessagePrefix(String targetId, String message) { String regex = "^" + Pattern.quote(targetId) + "_\\d+$"; Pattern pattern = Pattern.compile(regex); ONLINE_USER_SESSIONS.entrySet().stream().filter(entry -> { Matcher matcher = pattern.matcher(entry.getKey()); return matcher.matches(); }).map(Map.Entry::getValue).forEach(session -> PTZWebSocketUtils.sendMessage(session, message)); } }