|
@@ -2,10 +2,12 @@ package com.backendsys.modules.sse.utils;
|
|
|
|
|
|
import cn.hutool.core.convert.Convert;
|
|
import cn.hutool.core.convert.Convert;
|
|
import cn.hutool.json.JSONUtil;
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
+import com.backendsys.modules.common.config.security.utils.HttpRequestUtil;
|
|
import com.backendsys.modules.common.config.security.utils.SecurityUtil;
|
|
import com.backendsys.modules.common.config.security.utils.SecurityUtil;
|
|
import com.backendsys.modules.sse.emitter.SseEmitterManager;
|
|
import com.backendsys.modules.sse.emitter.SseEmitterManager;
|
|
import com.backendsys.modules.sse.entity.SseResponse;
|
|
import com.backendsys.modules.sse.entity.SseResponse;
|
|
import com.backendsys.modules.sse.entity.SseResponseEnum;
|
|
import com.backendsys.modules.sse.entity.SseResponseEnum;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
@@ -19,6 +21,9 @@ import java.util.concurrent.ConcurrentHashMap;
|
|
@Component
|
|
@Component
|
|
public class SseUtil {
|
|
public class SseUtil {
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private HttpRequestUtil httpRequestUtil;
|
|
|
|
+
|
|
@Value("${spring.application.name}")
|
|
@Value("${spring.application.name}")
|
|
private String APPLICATION_NAME;
|
|
private String APPLICATION_NAME;
|
|
|
|
|
|
@@ -41,16 +46,37 @@ public class SseUtil {
|
|
|
|
|
|
// [SSE] 发送消息 (单个) (自己)
|
|
// [SSE] 发送消息 (单个) (自己)
|
|
public void send(Long user_id, Object data) {
|
|
public void send(Long user_id, Object data) {
|
|
|
|
+
|
|
|
|
+ // 遍历所有 Emitter 并发送匹配前缀的目标 (拼接键值: 应用名称-用户ID-浏览器窗口UUID)
|
|
SseEmitterManager manager = SseEmitterManager.getInstance();
|
|
SseEmitterManager manager = SseEmitterManager.getInstance();
|
|
- SseEmitterUTF8 emitter = manager.getEmitter(APPLICATION_NAME + "-userid-" + Convert.toStr(user_id));
|
|
|
|
- if (emitter != null) {
|
|
|
|
- try {
|
|
|
|
- emitter.send(SseEmitter.event().data(data));
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- System.out.println(e.getMessage());
|
|
|
|
- manager.removeEmitter(emitter);
|
|
|
|
|
|
+ ConcurrentHashMap<String, SseEmitterUTF8> emitters = manager.getAllEmitters();
|
|
|
|
+
|
|
|
|
+ String prefix = APPLICATION_NAME + "-userid-" + Convert.toStr(user_id);
|
|
|
|
+ for (String key : emitters.keySet()) {
|
|
|
|
+ if (key.startsWith(prefix)) { // 检查是否满足前缀条件
|
|
|
|
+ SseEmitterUTF8 emitter = emitters.get(key);
|
|
|
|
+ if (emitter != null) {
|
|
|
|
+ try {
|
|
|
|
+ emitter.send(SseEmitter.event().data(data)); // 发送数据
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ System.out.println(e.getMessage());
|
|
|
|
+ manager.removeEmitter(emitter); // 发生异常时移除
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // 单个
|
|
|
|
+// SseEmitterManager manager = SseEmitterManager.getInstance();
|
|
|
|
+// SseEmitterUTF8 emitter = manager.getEmitter(APPLICATION_NAME + "-userid-" + Convert.toStr(user_id));
|
|
|
|
+// if (emitter != null) {
|
|
|
|
+// try {
|
|
|
|
+// emitter.send(SseEmitter.event().data(data));
|
|
|
|
+// } catch (IOException e) {
|
|
|
|
+// System.out.println(e.getMessage());
|
|
|
|
+// manager.removeEmitter(emitter);
|
|
|
|
+// }
|
|
|
|
+// }
|
|
}
|
|
}
|
|
|
|
|
|
// [SSE] 发送消息 (全部)
|
|
// [SSE] 发送消息 (全部)
|