孙永军 1 жил өмнө
parent
commit
72a487c8f5

+ 5 - 4
src/main/java/com/sxtvs/open/api/review/controller/SseController.java

@@ -4,12 +4,14 @@ import com.sxtvs.open.api.review.service.impl.SSEService;
 import com.sxtvs.open.core.auth.LoginRequired;
 import jakarta.annotation.Resource;
 import org.springframework.http.MediaType;
+import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
 
-@RestController
+@Controller
 @RequestMapping("/user")
 public class SseController {
 
@@ -17,10 +19,9 @@ public class SseController {
     private SSEService sseService;
 
     @GetMapping(path = "/sse", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
-    @LoginRequired
-    public SseEmitter handleSse() throws InterruptedException {
+    public SseEmitter handleSse(@RequestParam("token") String token) throws InterruptedException {
         SseEmitter sseEmitter = new SseEmitter(0L); // 设置超时时间
-        sseService.monitor(sseEmitter);
+        sseService.monitor(token, sseEmitter);
         Thread.sleep(500);
         return sseEmitter;
     }

+ 9 - 6
src/main/java/com/sxtvs/open/api/review/service/impl/SSEService.java

@@ -22,13 +22,16 @@ public class SSEService {
 
     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 void monitor(String token, SseEmitter sseEmitter) {
+        if (TextUtils.isEmpty(token)){
+            Optional<String> oToken = HttpContextUtil.getToken();
+            if (oToken.isPresent()){
+                token = oToken.get();
+            }
         }
+        String uid = AESUtil.decryptStr(token);
+        sseMap.put(Long.valueOf(uid), sseEmitter);
+
     }
 
     public Boolean sendMsg(Long uid, String msg) {