
本文旨在指导开发者如何在前端使用 Axios 处理 Server-Sent Events (SSE) 数据流,特别是在需要使用 POST 方法发送数据的情况下。文章将介绍一种基于 fetch-event-source 框架的解决方案,并提供其他可选方案,包括纯 JavaScript + Axios 的实现方式以及后端 Java (Spring) 的实现示例,以帮助读者全面理解和应用 SSE 技术。
Server-Sent Events (SSE) 是一种允许服务器单向推送数据到客户端的技术,非常适合实时更新的应用场景。虽然 EventSource API 是处理 SSE 的标准方法,但它不支持 POST 请求。因此,当需要使用 POST 方法与 SSE API 交互时,我们需要寻找替代方案。
fetch-event-source 是一个强大的 JavaScript 库,它基于 fetch API,提供了对 SSE 的支持,并且可以灵活地处理各种 HTTP 方法,包括 POST。
安装:
虽然原文没有给出安装方式,但通常可以通过 npm 或 yarn 安装:
npm install fetch-event-source # 或者 yarn add fetch-event-source
使用示例:
import { fetchEventSource } from '@microsoft/fetch-event-source';
const data = {
model: "gpt-3.5-turbo",
prompt: "Say this is a test",
max_tokens: 7,
stream: true,
temperature: 0
};
fetchEventSource('/my-service/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'text/event-stream', // 明确指定 Accept 类型
'SESSION_ID': 'Header-Value'
},
body: JSON.stringify(data),
onmessage(event) {
console.log("Received event:", event.data); // 处理接收到的数据
},
onerror(err) {
console.error("Error:", err);
},
onclose() {
console.log("Connection closed");
}
});注意事项:
另一种方法是结合使用 Axios 和 onDownloadProgress 回调函数,手动处理数据流。这种方法相对复杂,但可以避免引入额外的依赖。
axios({
url: '/my-service/chat', // 替换为你的API端点
data: {
prompt: 'json data'
},
headers: {
'accept': 'text/event-stream', // 明确指定 Accept 类型
'content-type': 'application/json'
},
method: 'POST',
responseType: 'stream', // 重要: 指定 responseType 为 'stream'
onDownloadProgress: progressEvent => {
const xhr = progressEvent.event.target;
const { responseText } = xhr;
console.log("=====responseText======");
console.log(responseText);
// 在这里处理接收到的数据流,例如解析 SSE 格式的数据
}
}).then(({ data }) => Promise.resolve(data));注意事项:
以下是使用 Spring Boot 创建 SSE 端点的示例代码:
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import java.time.Duration;
import java.time.LocalTime;
@RestController
public class SSEController {
@GetMapping(value = "/stream-sse", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ServerSentEvent<String>> streamSse() {
return Flux.interval(Duration.ofSeconds(1))
.map(sequence -> ServerSentEvent.<String>builder()
.id(String.valueOf(sequence))
.event("periodic-event")
.data("Server time: " + LocalTime.now())
.build());
}
}解释:
客户端测试:
可以使用 WebClient 测试该端点:
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import java.time.LocalTime;
public class SSEClient {
public static void main(String[] args) {
WebClient client = WebClient.create("http://localhost:8080");
Flux<ServerSentEvent<String>> eventStream = client.get()
.uri("/stream-sse")
.retrieve()
.bodyToFlux(ServerSentEvent.class);
eventStream.subscribe(
content -> System.out.println("Time: " + LocalTime.now() + " - event: name[" + content.event() + "], id [" + content.id() + "], content[" + content.data() + "] "),
error -> System.err.println("Error receiving SSE: " + error),
() -> System.out.println("Completed!!!"));
// Keep the application running for a while to receive events
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}本文介绍了在前端使用 Axios 处理 SSE 数据流的两种主要方法:使用 fetch-event-source 框架和使用纯 JavaScript + Axios。fetch-event-source 提供了更简洁易用的 API,而纯 JavaScript + Axios 则更加灵活,但需要手动处理数据流的解析。同时,文章也提供了后端 Java (Spring) 实现 SSE 端点的示例,帮助开发者构建完整的 SSE 应用。选择哪种方法取决于具体的应用场景和需求。在选择时,请考虑以下因素:
希望本文能够帮助你更好地理解和应用 SSE 技术。
以上就是使用 Axios 处理 Server-Sent Events (SSE) 数据流的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号