|
@@ -2,54 +2,59 @@ package com.backendsys.modules.log.controller;
|
|
|
|
|
|
import cn.hutool.core.convert.Convert;
|
|
import cn.hutool.core.convert.Convert;
|
|
import com.backendsys.modules.common.config.security.annotations.Anonymous;
|
|
import com.backendsys.modules.common.config.security.annotations.Anonymous;
|
|
-import com.backendsys.modules.log.emitter.LogStreamEmitterManager;
|
|
|
|
-import com.backendsys.modules.log.utils.LogStreamUtil;
|
|
|
|
|
|
+import com.backendsys.modules.sse.emitter.SseEmitterManager;
|
|
|
|
+import com.backendsys.modules.sse.utils.SseEmitterUTF8;
|
|
|
|
+import com.backendsys.modules.sse.utils.SseUtil;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
-import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
+import org.springframework.http.MediaType;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
-import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
|
|
|
|
-import javax.validation.constraints.NotNull;
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
@RestController
|
|
@RestController
|
|
public class LogStreamController {
|
|
public class LogStreamController {
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
- private LogStreamUtil logStreamUtil;
|
|
|
|
|
|
+ private SseUtil sseUtil;
|
|
|
|
|
|
/**
|
|
/**
|
|
* [SSE] 消息监听
|
|
* [SSE] 消息监听
|
|
*/
|
|
*/
|
|
@Anonymous
|
|
@Anonymous
|
|
@GetMapping(value = "/api/log/stream/watch", produces = "text/event-stream")
|
|
@GetMapping(value = "/api/log/stream/watch", produces = "text/event-stream")
|
|
- public SseEmitter stream(@RequestParam @NotNull(message = "v 不能为空") String v) {
|
|
|
|
-
|
|
|
|
|
|
+ public SseEmitter stream() {
|
|
String userId = Convert.toStr(1L);
|
|
String userId = Convert.toStr(1L);
|
|
- SseEmitter emitter = new SseEmitter(Long.MAX_VALUE);
|
|
|
|
- LogStreamEmitterManager manager = LogStreamEmitterManager.getInstance();
|
|
|
|
- manager.addEmitter(userId, emitter);
|
|
|
|
|
|
+ SseEmitterUTF8 emitter = new SseEmitterUTF8(Long.MAX_VALUE);
|
|
|
|
+ SseEmitterManager manager = SseEmitterManager.getInstance();
|
|
|
|
+
|
|
try {
|
|
try {
|
|
- emitter.send(SseEmitter.event().data("success"));
|
|
|
|
|
|
+
|
|
|
|
+ // 如果存在,则关闭
|
|
|
|
+ sseUtil.closeIfExist(userId);
|
|
|
|
+
|
|
|
|
+ // 创建新连接
|
|
|
|
+ manager.addEmitter(userId, emitter);
|
|
|
|
+ emitter.send(SseEmitter.event().data("Connected successfully! (连接成功)"));
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
- // 当所有事件发送完毕后,关闭连接
|
|
|
|
|
|
+ // 关闭连接
|
|
// emitter.complete();
|
|
// emitter.complete();
|
|
- // emitter.completeWithError(e);
|
|
|
|
manager.emitters.remove(emitter);
|
|
manager.emitters.remove(emitter);
|
|
}
|
|
}
|
|
return emitter;
|
|
return emitter;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* [SSE] 测试发送
|
|
* [SSE] 测试发送
|
|
*/
|
|
*/
|
|
@Anonymous
|
|
@Anonymous
|
|
@GetMapping("/api/log/stream/send")
|
|
@GetMapping("/api/log/stream/send")
|
|
public String send() {
|
|
public String send() {
|
|
- String message = "{\"message\": \"Hello World\"}";
|
|
|
|
- logStreamUtil.send(message);
|
|
|
|
|
|
+ String message = "{\"message\": \"Hello World 中文\"}";
|
|
|
|
+ sseUtil.send(message);
|
|
return "success";
|
|
return "success";
|
|
}
|
|
}
|
|
|
|
|