|
@@ -0,0 +1,48 @@
|
|
|
+package com.sxtvs.open.api.review.service.impl;
|
|
|
+
|
|
|
+import com.sxtvs.open.core.advice.BizException;
|
|
|
+import com.sxtvs.open.core.auth.AESUtil;
|
|
|
+import com.sxtvs.open.core.auth.HttpContextUtil;
|
|
|
+import jakarta.annotation.Resource;
|
|
|
+import org.apache.http.util.TextUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Optional;
|
|
|
+import java.util.concurrent.ThreadPoolExecutor;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class SSEService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ThreadPoolExecutor threadPoolExecutor;
|
|
|
+
|
|
|
+ private Map<Long, SseEmitter> sseMap = new HashMap<>();
|
|
|
+
|
|
|
+ public void monitor(SseEmitter sseEmitter) {
|
|
|
+ Optional<String> token = HttpContextUtil.getToken();
|
|
|
+
|
|
|
+ if (token.isPresent()){
|
|
|
+ String uid = AESUtil.decryptStr(token.get());
|
|
|
+ sseMap.put(Long.valueOf(uid), sseEmitter);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public Boolean sendMsg(Long uid, String msg) {
|
|
|
+ if (sseMap.containsKey(uid)){
|
|
|
+
|
|
|
+ SseEmitter sseEmitter = sseMap.get(uid);
|
|
|
+ try {
|
|
|
+ sseEmitter.send(msg);
|
|
|
+ return true;
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new BizException("推送消息失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+
|
|
|
+ }
|
|
|
+}
|