|
@@ -9,6 +9,7 @@ import com.backendsys.modules.sse.utils.SseEmitterUTF8;
|
|
import com.backendsys.modules.sse.utils.SseUtil;
|
|
import com.backendsys.modules.sse.utils.SseUtil;
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
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;
|
|
@@ -18,23 +19,27 @@ import java.io.IOException;
|
|
@RestController
|
|
@RestController
|
|
public class SseController {
|
|
public class SseController {
|
|
|
|
|
|
|
|
+ @Value("${spring.application.name}")
|
|
|
|
+ private String APPLICATION_NAME;
|
|
|
|
+
|
|
@Autowired
|
|
@Autowired
|
|
private SseUtil sseUtil;
|
|
private SseUtil sseUtil;
|
|
|
|
|
|
/**
|
|
/**
|
|
- * [SSE] 消息监听
|
|
|
|
|
|
+ * [SSE] 消息监听 (拼接键值: 应用名称-用户ID)
|
|
*/
|
|
*/
|
|
- @GetMapping(value = "/api/sse/stream", produces = "text/event-stream;charset=UTF-8")
|
|
|
|
|
|
+ @GetMapping(value = "/api/sse/listenStream", produces = "text/event-stream;charset=UTF-8")
|
|
public SseEmitter stream(HttpServletResponse response) {
|
|
public SseEmitter stream(HttpServletResponse response) {
|
|
- // 获得当前用户Id
|
|
|
|
- String userId = Convert.toStr(SecurityUtil.getUserId());
|
|
|
|
|
|
+
|
|
|
|
+ // 拼接键值: 应用名称-用户ID
|
|
|
|
+ String emitterKey = APPLICATION_NAME + "-userid-" + Convert.toStr(SecurityUtil.getUserId());
|
|
|
|
|
|
// 如果存在,则关闭
|
|
// 如果存在,则关闭
|
|
- sseUtil.closeIfExist(userId);
|
|
|
|
|
|
+ sseUtil.closeIfExist(emitterKey);
|
|
|
|
|
|
SseEmitterUTF8 emitter = new SseEmitterUTF8(Long.MAX_VALUE);
|
|
SseEmitterUTF8 emitter = new SseEmitterUTF8(Long.MAX_VALUE);
|
|
SseEmitterManager manager = SseEmitterManager.getInstance();
|
|
SseEmitterManager manager = SseEmitterManager.getInstance();
|
|
- manager.addEmitter(userId, emitter);
|
|
|
|
|
|
+ manager.addEmitter(emitterKey, emitter);
|
|
try {
|
|
try {
|
|
// 设置响应头,指定字符编码
|
|
// 设置响应头,指定字符编码
|
|
response.setCharacterEncoding("UTF-8");
|
|
response.setCharacterEncoding("UTF-8");
|
|
@@ -45,19 +50,30 @@ public class SseController {
|
|
|
|
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
// 关闭连接
|
|
// 关闭连接
|
|
- System.out.println("(/api/sse/stream)(controller):" + e.getMessage());
|
|
|
|
|
|
+ System.out.println("(listenStream):" + e.getMessage());
|
|
manager.removeEmitter(emitter);
|
|
manager.removeEmitter(emitter);
|
|
}
|
|
}
|
|
return emitter;
|
|
return emitter;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * [SSE] 测试发送
|
|
|
|
|
|
+ * [SSE] 测试发送 (单个)
|
|
*/
|
|
*/
|
|
- @GetMapping("/api/sse/send")
|
|
|
|
- public String send() {
|
|
|
|
|
|
+ @GetMapping("/api/sse/sendHello")
|
|
|
|
+ public String sendHelloWorld() {
|
|
String dataStr = (new SseResponse("Hello World")).toJsonStr();
|
|
String dataStr = (new SseResponse("Hello World")).toJsonStr();
|
|
- sseUtil.send(SecurityUtil.getUserId(), dataStr);
|
|
|
|
|
|
+ String emitterKey = APPLICATION_NAME + "-userid-" + Convert.toStr(SecurityUtil.getUserId());
|
|
|
|
+ sseUtil.send(emitterKey, dataStr);
|
|
|
|
+ return "success";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * [SSE] 测试发送 (全部)
|
|
|
|
+ */
|
|
|
|
+ @GetMapping("/api/sse/sendHelloToAll")
|
|
|
|
+ public String sendHelloWorldToAll() {
|
|
|
|
+ String dataStr = (new SseResponse("Hello World Everyone")).toJsonStr();
|
|
|
|
+ sseUtil.sendToAll(dataStr);
|
|
return "success";
|
|
return "success";
|
|
}
|
|
}
|
|
|
|
|