当前位置:网站首页>boot - SSE
boot - SSE
2022-08-03 06:25:00 【iiaythi】
boot - SSE 使用
sse(
Server Sent Event),直译为服务器发送事件,顾名思义,也就是客户端可以获取到服务器发送的事件
webflux 使用
@GetMapping("/stream-sse")
public Flux<ServerSentEvent<String>> streamEvents() {
return Flux.interval(Duration.ofSeconds(1))
.map(sequence -> ServerSentEvent.<String> builder()
.id(String.valueOf(sequence))
.event("periodic-event")
.data("SSE - " + LocalTime.now().toString())
.build());
}
@GetMapping(path = "/stream-flux", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> streamFlux() {
return Flux.interval(Duration.ofSeconds(1))
.map(sequence -> "Flux - " + LocalTime.now().toString());
}
spring-mvc 方式
@GetMapping("/stream-sse-mvc")
public SseEmitter streamSseMvc() {
SseEmitter emitter = new SseEmitter();
ExecutorService sseMvcExecutor = Executors.newSingleThreadExecutor();
sseMvcExecutor.execute(() -> {
try {
for (int i = 0; true; i++) {
SseEmitter.SseEventBuilder event = SseEmitter.event()
.data("SSE MVC - " + LocalTime.now().toString())
.id(String.valueOf(i))
.name("sse event - mvc");
emitter.send(event);
Thread.sleep(1000);
}
} catch (Exception ex) {
emitter.completeWithError(ex);
}
});
return emitter;
}
边栏推荐
- 七夕和程序员有毛关系?
- excel高级绘图技巧100讲(二十一)- Excel层叠柱形图
- 一篇文章教你写扫雷(c语言基础版)
- Chrome插件开发入门
- 开放域OOD主要数据集、评价指标汇总
- 信息学奥赛一本通T1449:魔板
- 1066 Root of AVL Tree // AVL平衡二叉搜索树模板
- 分布式数据库数据一致性的原理、与技术实现方案
- qt学习之旅--MinGW编译FFmpeg(32bit)
- El - tree to set focus on selected highlight highlighting, the selected node deepen background and change the font color, etc
猜你喜欢
随机推荐
Chrome 配置samesite=none方式
empty() received an invalid combination of arguments - got (tuple, dtype=NoneType, device=NoneType),
Cesium加载离线地图和离线地形
CDGA|如何加强数字政府建设?
1066 Root of AVL Tree // AVL平衡二叉搜索树模板
我国有关信息方面的法律法规
Nacos与Eureka的区别
信息学奥赛一本通T1446:素数方阵
Nacos单机模式的安装与启动
Nacos下载与安装
excel高级绘图技巧100讲(二十一)- Excel层叠柱形图
最新版图书馆招聘考试常考试题重点事业单位
10 common data types in MySQL
【RT_Thread学习笔记】---以太网LAN8720A Lwip ping 通网络
神经网络原理及代码实现
El - tree to set focus on selected highlight highlighting, the selected node deepen background and change the font color, etc
伦敦银现货市场如何使用多条均线?
MySQL - 触发器
el-tree设置利用setCheckedNodessetCheckedKeys默认勾选节点,以及通过setChecked新增勾选指定节点
Docker-compose安装mysql









