[英]Spring Webflux Rest Controller for image response correct return type
[英]Server Sent Event Spring Webflux return event name in response from rest controller
我正在使用 SSE 和 reactor Flux 来捕获 linux 系统指标并发布到浏览器(javascript eventsource)。
目前,问题是无法将特定数据发送到特定客户端。 在通过多个 SO 帖子后,它被告知在事件源中使用事件侦听器,并且来自服务器的响应应将事件作为响应中的关键。
因为,我从服务器返回一个对象,将事件作为响应 json 中的键之一就足够了,响应 json 将由事件源标识。
在客户端设置一个唯一编号,该编号将在 json 响应中的事件键中返回。
谢谢您的帮助 !
Javascript:
jsonStreamObjectHeap.addEventListener("197e08e-f9a4-4e6e-9a04-220ade08a8f4",function(e){
$.each(message, function(index, value) {
/*some operation*/
});
}
来自休息控制器的响应:
{
"event":"197e08e-f9a4-4e6e-9a04-220ade08a8f4",
"data":"2048"
}
休息控制器代码:
Flux<Long> interval = Flux.interval(Duration.ofSeconds(1));
interval.subscribe((i) -> testStreamList.forEach(testStream -> {
try {
generateTestStream(testStream, UUID);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}));
Flux<List<TestStream>> transactionFlux = Flux.fromStream(Stream.generate(() -> testStreamList));
return Flux.zip(interval, transactionFlux).map(Tuple2::getT2);
我发现org.springframework.http.codec.ServerSentEvent
可以插入来自其余控制器的 Flux 响应。
Flux<Long> interval = Flux.interval(Duration.ofSeconds(1));
Flux<HeapStat> transactionFlux = Flux.fromStream(Stream.generate(() -> heapStat));
/*Bulding ServerSentEvent to from the tuple*/
return Flux.zip(interval, transactionFlux).map(tuple->
ServerSentEvent.<HeapStat>builder().event(jsessionId).data(tuple.getT2()).build()
);
您可以使用自定义事件名称 Last-Event-ID 完全像SSEEmmiter
一样准备响应
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.