|
@@ -21,24 +21,20 @@ public class SseController {
|
|
|
/**
|
|
|
* [SSE] 消息监听
|
|
|
*/
|
|
|
- private final ExecutorService executor = Executors.newCachedThreadPool();
|
|
|
-
|
|
|
@GetMapping(value = "/api/sse/stream", produces = "text/event-stream")
|
|
|
public SseEmitter stream() {
|
|
|
String userId = Convert.toStr(1L);
|
|
|
SseEmitter emitter = new SseEmitter(Long.MAX_VALUE);
|
|
|
SseEmitterManager manager = SseEmitterManager.getInstance();
|
|
|
manager.addEmitter(userId, emitter);
|
|
|
- executor.execute(() -> {
|
|
|
- try {
|
|
|
- emitter.send(SseEmitter.event().data("success"));
|
|
|
- } catch (IOException e) {
|
|
|
- // 当所有事件发送完毕后,关闭连接
|
|
|
- // emitter.complete();
|
|
|
- // emitter.completeWithError(e);
|
|
|
- manager.emitters.remove(emitter);
|
|
|
- }
|
|
|
- });
|
|
|
+ try {
|
|
|
+ emitter.send(SseEmitter.event().data("success"));
|
|
|
+ } catch (IOException e) {
|
|
|
+ // 当所有事件发送完毕后,关闭连接
|
|
|
+ // emitter.complete();
|
|
|
+ // emitter.completeWithError(e);
|
|
|
+ manager.emitters.remove(emitter);
|
|
|
+ }
|
|
|
return emitter;
|
|
|
}
|
|
|
|
|
@@ -47,7 +43,8 @@ public class SseController {
|
|
|
*/
|
|
|
@GetMapping("/api/sse/send")
|
|
|
public String send() {
|
|
|
- sseUtil.send("send message!");
|
|
|
+ String message = "{\"message\": \"Hello World\"}";
|
|
|
+ sseUtil.send(message);
|
|
|
return "success";
|
|
|
}
|
|
|
|