SseController.java 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package com.backendsys.modules.sse.controller;
  2. import cn.hutool.core.convert.Convert;
  3. import com.backendsys.modules.common.config.security.utils.HttpRequestUtil;
  4. import com.backendsys.modules.common.config.security.utils.SecurityUtil;
  5. import com.backendsys.modules.sse.emitter.SseEmitterManager;
  6. import com.backendsys.modules.sse.entity.SseResponse;
  7. import com.backendsys.modules.sse.entity.SseResponseEnum;
  8. import com.backendsys.modules.sse.utils.SseEmitterUTF8;
  9. import com.backendsys.modules.sse.utils.SseUtil;
  10. import jakarta.servlet.http.HttpServletResponse;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.beans.factory.annotation.Value;
  13. import org.springframework.security.core.Authentication;
  14. import org.springframework.security.core.context.SecurityContextHolder;
  15. import org.springframework.web.bind.annotation.GetMapping;
  16. import org.springframework.web.bind.annotation.RequestHeader;
  17. import org.springframework.web.bind.annotation.RestController;
  18. import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
  19. import java.io.IOException;
  20. @RestController
  21. public class SseController {
  22. @Value("${spring.application.name}")
  23. private String APPLICATION_NAME;
  24. @Autowired
  25. private SseUtil sseUtil;
  26. @Autowired
  27. private HttpRequestUtil httpRequestUtil;
  28. /**
  29. * [SSE] 消息监听 (拼接键值: 应用名称-用户ID)
  30. */
  31. @GetMapping(value = "/api/sse/listenStream", produces = "text/event-stream;charset=UTF-8")
  32. public SseEmitter stream(HttpServletResponse response) {
  33. // 获得 浏览器窗口UUID
  34. String browserWindowUUID = httpRequestUtil.getBrowserWindowUUID();
  35. // 拼接键值: 应用名称-用户ID-浏览器窗口UUID
  36. String emitterKey = APPLICATION_NAME + "-userid-" + Convert.toStr(SecurityUtil.getUserId() + "-" + browserWindowUUID);
  37. // 如果存在,则关闭
  38. sseUtil.closeIfExist(emitterKey);
  39. // System.out.println("browserWindowUUID = " + browserWindowUUID);
  40. // System.out.println("emitterKey = " + emitterKey);
  41. SseEmitterUTF8 emitter = new SseEmitterUTF8(Long.MAX_VALUE);
  42. SseEmitterManager manager = SseEmitterManager.getInstance();
  43. manager.addEmitter(emitterKey, emitter);
  44. try {
  45. // 设置响应头,指定字符编码
  46. response.setCharacterEncoding("UTF-8");
  47. response.setContentType("text/event-stream");
  48. // CONNECT: 建立连接
  49. String dataStr = (new SseResponse(SseResponseEnum.CONNECT)).toJsonStr();
  50. emitter.send(SseEmitter.event().data(dataStr));
  51. } catch (IOException e) {
  52. // 关闭连接
  53. System.out.println("(listenStream):" + e.getMessage());
  54. manager.removeEmitter(emitter);
  55. }
  56. return emitter;
  57. }
  58. /**
  59. * [SSE] 测试发送 (单个)
  60. */
  61. @GetMapping("/api/sse/sendHello")
  62. public String sendHelloWorld() {
  63. String dataStr = (new SseResponse("Hello World")).toJsonStr();
  64. sseUtil.send(SecurityUtil.getUserId(), dataStr);
  65. return "success";
  66. }
  67. /**
  68. * [SSE] 测试发送 (全部)
  69. */
  70. @GetMapping("/api/sse/sendHelloToAll")
  71. public String sendHelloWorldToAll() {
  72. String dataStr = (new SseResponse("Hello World Everyone")).toJsonStr();
  73. sseUtil.sendToAll(dataStr);
  74. return "success";
  75. }
  76. }