当前位置:网站首页>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;
}
边栏推荐
- Nacos与Eureka的区别
- Cesium loads offline maps and offline terrain
- 多线程案例
- ISIJ 2022收官,中国初中生再展风采
- qt学习之旅--MinGW编译FFmpeg(32bit)
- QT信号与槽
- 信息学奥赛一本通T1450:Knight Moves
- empty() received an invalid combination of arguments - got (tuple, dtype=NoneType, device=NoneType),
- Chrome configuration samesite=none method
- El - table column filter functions, control columns show and hide (effect and easy to implement full marks)
猜你喜欢

关于Attention的超详细讲解

c现代方法16章基础

DAC、ADC、FFT使用总结

模型训练前后显卡占用对比、多卡训练GPU占用分析【一文读懂】

el-table gets the data attribute of a row in the read data table

数据仓库指标体系实践

现货黄金分析的主要流派

【playwright】pytest-playwright增加代理服务选项

Spark 的架构与作业提交流程

El - tree set using setCheckedNodessetCheckedKeys default check nodes, and a new check through setChecked specified node
随机推荐
Chrome configuration samesite=none method
qt学习之旅--MinGW编译FFmpeg(32bit)
JS作用对象API技巧
docker-compose部署mysql
Flutter | 判断 Text 组件是否显示完
Week5
mysql or语句的优化
用代码构建UI界面
最新版图书馆招聘考试常考试题重点事业单位
一文搞懂什么是@Component和@Bean注解以及如何使用
模型训练前后显卡占用对比、多卡训练GPU占用分析【一文读懂】
spark中的cache和checkpoint
mysql慢查询优化
异常检测 IsolationForest 返回概率
关于Attention的超详细讲解
2022用户画像构建
6.nodejs--promise、async-await
2022年 SQL 优化大全总结详解
信息学奥赛一本通T1452:Keyboarding
word之图表目录中点号位置提升3磅