当前位置:网站首页>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;
}
边栏推荐
- Example of embedding code for continuous features
- 重量级大咖来袭:阿里云生命科学与智能计算峰会精彩内容剧透
- el-tree设置选中高亮焦点高亮、选中的节点加深背景,更改字体颜色等
- Pinned Articles-
- Spark 的架构与作业提交流程
- PHP 获取服务器信息
- El - table column filter functions, control columns show and hide (effect and easy to implement full marks)
- 解读 refresh 十二步骤
- 第四章:架构,Architecture
- FiBiNet torch reproduction
猜你喜欢
随机推荐
1066 Root of AVL Tree // AVL平衡二叉搜索树模板
AlexNet网络详解及复现
第一章:ARM公司Cortex-M 系列处理器介绍,第二章:嵌入式软件开发介绍和第三章:Cortex-M3和Cortex-M4处理器的一般介绍
安全狗云原生安全能力全面亮相全球数字经济大会暨ISC互联网安全大会
华为设备配置BFD多跳检测
idea远程debug
线程基础(二)
CDGA|如何加强数字政府建设?
多线程打印ABC(继承+进阶)
pyspark @udf 循环使用变量问题
IEEE RAL投初稿
mysql or语句的优化
信息学奥赛一本通T1453:移动玩具
MySQL的 DDL和DML和DQL的基本语法
【着色器实现HandDrawn简笔画抖动效果_Shader效果第十二篇】
Week5
unity 摄像机旋转拖拽缩放场景
pyspark df 二次排序
qt学习之旅--MinGW32编译opencv3.0.0
华为设备配置BFD状态与接口状态联动









