服务器发送事件 spring webFlux with reactor

server sent event spring webFlux with reactor

这是否是通过浏览器客户端发送常见主题信息的正确方法?

@RestController
public class GenvScriptHandler {

    DirectProcessor<String> topicData = DirectProcessor.create();
    FluxSink<String> sink;
    int test;


    @GetMapping(value = "/addTopic")
    public void addTopic() {
        if (sink == null) {
            sink = topicData.sink();
        }
        sink.next(String.valueOf(test++));
    }

    @GetMapping(value = "/getTopic", produces = "text/event-stream")
    public Flux<String> getTopic() {
        Flux<String> autoConnect = topicData.publish().autoConnect();

        return autoConnect;
    }
}

当我使用 DirectProcessor 时,不可能有背压,我想知道通过 sse 发送时通量是如何消耗的。订户是否可以请求小于通量中推送的元素数量?

http://projectreactor.io/docs/core/release/reference/#_directprocessor

As a consequence, a DirectProcessor signals an IllegalStateException to its subscribers if you push N elements through it but at least one of its subscribers has requested less than N.

订阅 SSE 请求,执行 请求(1) 而不是请求(Integer.MAX_VALUE)

因此,如果我下沉 * 1000 次,处理器过载并抛出异常,即使它有订阅者:

reactor.core.Exceptions$OverflowException: Can't deliver value due to lack of requests

在我的案例中使用 EmitterProcessor 或 ReplayProcessor 更安全