From 2ab35000026ccd58238e6a504b1b5f79f8c262c3 Mon Sep 17 00:00:00 2001
From: liusuyi <1951119284@qq.com>
Date: 星期三, 17 七月 2024 10:20:20 +0800
Subject: [PATCH] 优化
---
ard-work/src/main/java/com/ruoyi/utils/websocket/util/WebSocketUtils.java | 65 ++++++++++++++++++++++++++------
1 files changed, 53 insertions(+), 12 deletions(-)
diff --git a/ard-work/src/main/java/com/ruoyi/utils/websocket/util/WebSocketUtils.java b/ard-work/src/main/java/com/ruoyi/utils/websocket/util/WebSocketUtils.java
index 5a2b3e6..4583f87 100644
--- a/ard-work/src/main/java/com/ruoyi/utils/websocket/util/WebSocketUtils.java
+++ b/ard-work/src/main/java/com/ruoyi/utils/websocket/util/WebSocketUtils.java
@@ -2,14 +2,15 @@
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
+import org.springframework.web.socket.WebSocketSession;
import javax.websocket.RemoteEndpoint;
import javax.websocket.Session;
import java.io.IOException;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
import java.util.concurrent.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
/**
* @ClassName WebSocketUtils
@@ -23,6 +24,8 @@
// 瀛樺偍 websocket session
public static final Map<String, Session> ONLINE_USER_SESSIONS = new ConcurrentHashMap<>();
+ //瀛樺偍鎴块棿
+ public static final ConcurrentHashMap<String, Set<String>> ROOM_USER_SET = new ConcurrentHashMap<>();
/**
* @param session 鐢ㄦ埛 session
@@ -36,15 +39,16 @@
if (basic == null) {
return;
}
- synchronized(session) {
+ synchronized (session) {
try {
- log.debug("鍙戦�佹秷鎭細"+message);
+ log.debug("鍙戦�佹秷鎭細" + message);
session.getBasicRemote().sendText(message);
} catch (IOException e) {
- log.error("sendMessage IOException ",e);
+ log.error("sendMessage IOException ", e);
}
}
}
+
/**
* @param session 鐢ㄦ埛 session
* @param message 鍙戦�佸唴瀹�
@@ -57,14 +61,15 @@
if (basic == null) {
return;
}
- synchronized(session) {
+ synchronized (session) {
try {
session.getBasicRemote().sendText(new JSONObject(message).toString());
} catch (IOException e) {
- log.error("sendMessage IOException ",e);
+ log.error("sendMessage IOException ", e);
}
}
}
+
public static void sendMessage(Session session, List message) {
if (session == null) {
return;
@@ -73,27 +78,63 @@
if (basic == null) {
return;
}
- synchronized(session) {
+ synchronized (session) {
try {
- session.getBasicRemote().sendText( String.join(", ", message));
+ session.getBasicRemote().sendText(String.join(", ", message));
} catch (IOException e) {
- log.error("sendMessage 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);
+ JSONObject jsonObject = new JSONObject(message);
ONLINE_USER_SESSIONS.forEach((sessionId, session) -> sendMessage(session, jsonObject.toString()));
}
+
+ /**
+ * 鎺ㄩ�佹秷鎭埌褰撳墠鎴块棿鐨勫叾浠栧鎴风
+ *
+ * @param message
+ */
+ public static void sendRoomMessage(String roomId, String message) {
+ Set<String> userSet = ROOM_USER_SET.getOrDefault(roomId, new HashSet<>());
+ userSet.stream().forEach(userId -> {
+ String regex = "^" + Pattern.quote(userId) + "_\\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 -> WebSocketUtils.sendMessage(session, 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 -> WebSocketUtils.sendMessage(session, message));
+ }
}
\ No newline at end of file
--
Gitblit v1.9.3