当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
el-tree设置选中高亮焦点高亮、选中的节点加深背景,更改字体颜色等
重量级大咖来袭:阿里云生命科学与智能计算峰会精彩内容剧透
用代码构建UI界面
IEEE RAL投初稿
MySQL的DATE_FORMAT()函数将Date转为字符串
Detailed explanation of AutoInt network and pytorch reproduction
华为设备配置BFD与接口联动(触发与BFD联动的接口物理状态变为Down)
【卫朋】硬件创业:营销与开发同行
Autowired注解与Resource注解的区别
6.nodejs--promise、async-await
DIFM网络详解及复现
pt-online-schema-change工具使用的一次
力扣解法汇总622-设计循环队列
Embedding two implementations of the torch code
pyspark---低频特征处理
Charles抓包显示<unknown>解决方案
MySQL性能优化(硬件,系统配置,表结构,SQL语句)
Example of embedding code for continuous features
开放域OOD主要数据集、评价指标汇总
1066 Root of AVL Tree // AVL平衡二叉搜索树模板