【发布时间】:2022-08-14 05:03:58
【问题描述】:
我正在尝试实现一个服务器发送事件控制器,以使用要显示的最新数据更新我的 Web 浏览器客户端。
这是我当前的控制器,它每 5 秒发送一次我的数据列表。每次我将数据保存在另一个服务中时,我都想发送一个 SSE。 我阅读了有关使用通道的信息,但是如何使用 Flux 使用它?
@GetMapping(\"/images-sse\")
fun getImagesAsSSE(
request: HttpServletRequest
): Flux<ServerSentEvent<MutableList<Image>>> {
val subdomain = request.serverName.split(\".\").first()
return Flux.interval(Duration.ofSeconds(5))
.map {
ServerSentEvent.builder<MutableList<Image>>()
.event(\"periodic-event\")
.data(weddingService.getBySubdomain(subdomain)?.pictures).build()
}
}
-
不要使用处理器,因为它已被弃用。水槽是个好主意。我会发布样本。
标签: spring-boot kotlin spring-webflux project-reactor