当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
Shell脚本之一键安装mysql
《多线程案例》阻塞队列、定时器、线程池、饿汉与懒汉模式
亿流量大考(1):日增上亿数据,把MySQL直接搞宕机了...
请手撸5种常见限流算法!面试必备
开放域OOD主要数据集、评价指标汇总
DAC、ADC、FFT使用总结
Cesium加载离线地图和离线地形
nacos-2.0.3启动报错出现no datasource set的坑
伦敦银现货市场如何使用多条均线?
JS 预编译
海思项目总结
el-table实现列筛选功能,控制列的显示和隐藏(实现简单,效果满分)
MySQL忘记密码怎么办
死锁的成因和对应的解决方案
qt学习之旅--MinGW32编译opencv3.0.0
CISP-PTE真题演示
IFM network detailed explanation and torch reproduction
解读 refresh 十二步骤
mongodb的shell脚本
信息学奥赛一本通T1447:靶形数独









