当前位置:网站首页>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;
}
边栏推荐
- Pinned Articles-
- 【卫朋】硬件创业:营销与开发同行
- hashSet解析
- el-tree设置利用setCheckedNodessetCheckedKeys默认勾选节点,以及通过setChecked新增勾选指定节点
- El - tree set using setCheckedNodessetCheckedKeys default check nodes, and a new check through setChecked specified node
- spark中的bykey
- mongodb的shell脚本
- 置顶文章-
- Example of embedding code for continuous features
- Cesium loads offline maps and offline terrain
猜你喜欢

Getting started with el-tabs (tab bar)

CISP-PTE真题演示

线程基础(二)

El - tree set using setCheckedNodessetCheckedKeys default check nodes, and a new check through setChecked specified node

MySQL的DATE_FORMAT()函数将Date转为字符串

关于Attention的超详细讲解

El - table column filter functions, control columns show and hide (effect and easy to implement full marks)

Nacos下载与安装

Multi-Head-Attention principle and code implementation

QT信号与槽
随机推荐
CDGA|如何加强数字政府建设?
MySQL日期和时间戳的转换
关于任命韩文弢博士代理NOI科学委员会主席的公告
[机缘参悟-59]:《素书》-6-安于礼仪[安礼章第六]
pyspark---对suuid区间编码(基于曝光数、点击数)
DIFM network, rounding and repetition
Multi-Head-Attention原理及代码实现
C语言版本和GCC版本
七夕和程序员有毛关系?
MySQL 流程控制
帆软11版本参数联动为null查询全部
用代码构建UI界面
多线程可见
MYSQL存储过程注释详解
神经网络原理及代码实现
在线开启gtid偶发hang住的问题解决
Detailed explanation of AutoInt network and pytorch reproduction
Shell脚本之一键安装mysql
信息学奥赛一本通T1449:魔板
spark中的cache和checkpoint