当前位置: 首页 > news >正文

SseEmitter

SseEmitter是spring框架中用于处理服务器发送事件的一个工具类,,
sse : server sent event
服务器主动推送到客户端,,,

spring事件基于观察者模式,也可以看作一种发布订阅,,
有消息推送之后,会去遍历每个消费者,也就是监听器,去消费信息,,,,,

客户端首先将自己添加为消费者,,等到某个事件发布的时候,去触发对应的消费者,,执行

页面加载的时候,就去发送一个请求,将自己变成消费者,,在离开页面的时候,把自己从消费者中移除出去

遇到的问题,,google浏览器,不让直接播放mp3,,,
document点击之后,去设置这个audio的src,,然后就能播放
解决:https://blog.csdn.net/w1299395323/article/details/107332861/

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<button onclick="handleClick()">play</button>
<audio  id="audio"  src="tipsMp3.mp3">
<!-- <source src="tipsMp3.mp3" type="audio/mp3">-->
</audio><script>var audioDom = document.querySelector("#audio");// audioDom.src="./tipsMp3.mp3"document.addEventListener("click",function (event){console.log("hehe")audioDom.src="./tipsMp3.mp3"})const eventSource =  new EventSource("http://localhost:8092/manager/sse/connection/123")eventSource.onmessage = function (event){if (event.data ==="Heartbeat"){return}console.log(event,"event")console.log("收到消息",event.data)audioDom.play()}eventSource.onerror = function (event){console.log(event,"event","err")if (eventSource.readyState === EventSource.CLOSED){console.log("连接已关闭")}}eventSource.onopen = function (){console.log("连接已打开")}
</script>
</body>
</html>
package com.cj.sse;import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;import java.io.IOException;
import java.util.Map;
import java.util.concurrent.*;@Service
@Slf4j
public class SseService {
// 存监听器,,Map<String, SseEmitter> sseCache = new ConcurrentHashMap<>();public SseEmitter connect(String clientId) {SseEmitter sseEmitter = new SseEmitter(0L);ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);// 30秒一次,延迟0秒开始scheduler.scheduleAtFixedRate(()->{// 创建了一个新的事件,,名字是message,发送heartbeattry {sseEmitter.send(SseEmitter.event().name("message").data("Heartbeat"));} catch (IOException e) {throw new RuntimeException(e);}},0,30, TimeUnit.MINUTES);sseCache.put(clientId,sseEmitter);System.out.println(sseCache+"---"+sseCache.size());return sseEmitter;}/*** 发送消息 : 发送给指定的Emitter,也可以是所有*/public void sendMessageToAll(String message){try {log.info("发送消息all:{}",message);for (SseEmitter emitter : sseCache.values()) {emitter.send(message);}} catch (IOException e) {log.error("发送消息失败",e);}}/*** 打开页面的时候 监听,,,, 离开页面的时候,移除监听* @param userId*/public  void closeSse(String userId){if (sseCache.containsKey(userId)){SseEmitter sseEmitter = sseCache.get(userId);sseEmitter.complete();sseCache.remove(userId);}else{log.info("用户{} 已关闭",userId);}}
}

http://www.mrgr.cn/news/5166.html

相关文章:

  • Nginx+certbot 免费Https证书
  • 实现高效研发运营一体化:深度落地DevOps解决方案的探索与实践
  • Wot Design Uni:一个高颜值、轻量化的uni-app组件库,uni-app生态的新宠
  • 2025计算机毕设:50条小众好做的SSM题目推荐【计算机毕设选题推荐】
  • 数学建模之数据分析【七】:对Pandas DataFrame 进行切片
  • LCD 驱动
  • 《python语言程序设计》2018版第8章第2题检查子串, 使用str类的find方法检查一个字符串是否是另一个字符串的子串
  • 商业软件许可证介绍|简单原理探究
  • 6.MySQL的增删改查
  • QT中使用QAxObject类读取xlsx文件内容并显示在ui界面
  • 【K8s】Java项目部署时为什么要用k8s?
  • 身份证OCR-身份证OCR识别-身份证OCR文字识别-身份证识别--身份证图像识别-身份证信息识别接口
  • python实现链表
  • 设计模式-责任链模式
  • Python类的介绍
  • 实现Linux的高可用负载均衡
  • 【云原生】企业级WEB应用服务器TOMCAT
  • 在VS Code中使用Snippet Craft扩展提高编码效率
  • 论文阅读:MonoScene: Monocular 3D Semantic Scene Completion
  • vue生命周期函数