SpringBoot 搭建 SSE

参考链接

https://www.51cto.com/article/798001.html

了解一下SseEmitter(一)-CSDN博客

依赖

有默认的 springboot-web 依赖即可

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

SSEmitter API

方法

说明

onCompletion()

回调方法,连接完成(正常关闭)时触发(超时,异常,complete() 之后触发)

onTimeout()

回调方法,当连接超时时触发

onError()

指定当发生错误时执行的回调方法。这个错误可能是由于网络连接问题等原因。

completeWithError(e)

在发生错误时关闭连接,并以错误的形式告知客户端

complete()

表示数据发送完成后关闭连接

event()

创建一个事件对象,设置事件名称和数据

使用示例emitter.send(SseEmitter.event().name("message").data("数据!"));

onCompletion() 回调函数触发条