当前位置:网站首页>boot-SSE
boot-SSE
2022-08-03 07:32: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;
}
边栏推荐
- (十四)51单片机——LCD1602实现滚动效果
- 死锁的成因和对应的解决方案
- empty() received an invalid combination of arguments - got (tuple, dtype=NoneType, device=NoneType),
- 用代码构建UI界面
- mongodb的shell脚本
- empty() received an invalid combination of arguments - got (tuple, dtype=NoneType, device=NoneType),
- 华为设备配置BFD状态与接口状态联动
- 连续型特征做embedding代码示例
- Spark 的架构与作业提交流程
- excel高级绘图技巧100讲(二十一)- Excel层叠柱形图
猜你喜欢
随机推荐
开放域OOD主要数据集、评价指标汇总
编程语言有什么
华为设备配置BFD多跳检测
MySQL - 视图操作
解决plt.imshow()不显示图片cv2.imshw()不显示图片
spark中Repartition 和 Coalesce 区别
MySQL性能优化(硬件,系统配置,表结构,SQL语句)
Nacos下载与安装
Charles capture shows
solution 信息学奥赛一本通T1447:靶形数独
DAC、ADC、FFT使用总结
pyspark df 二次排序
被数据分析重塑的5个行业
模型训练前后显卡占用对比、多卡训练GPU占用分析【一文读懂】
Nacos与Eureka的区别
MYSQL存储过程注释详解
idea远程debug
Spark 的架构与作业提交流程
华为设备配置BFD单跳检测二层链路
阿里云-武林头条-建站小能手争霸赛








