当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
postman将接口返回结果生成csv文件到本地
pyspark df 二次排序
word之图表目录中点号位置提升3磅
spark中的bykey
海思项目总结
Week5
uniapp 请求接口封装
力扣解法汇总622-设计循环队列
请手撸5种常见限流算法!面试必备
IFM network detailed explanation and torch reproduction
关于任命韩文弢博士代理NOI科学委员会主席的公告
重量级大咖来袭:阿里云生命科学与智能计算峰会精彩内容剧透
链表之打基础--基本操作(必会)
关于NOI 2022的报到通知
信息学奥赛一本通T1453:移动玩具
异常检测 IsolationForest 返回概率
Cesium loads offline maps and offline terrain
El - tree to set focus on selected highlight highlighting, the selected node deepen background and change the font color, etc
Charles capture shows
solution 【着色器实现HandDrawn简笔画抖动效果_Shader效果第十二篇】









