1. 安装依赖
| npx expo install react-native-sse |
react-native-sse Github地址
2. 引入
| import EventSource from "react-native-sse"; |
3. 创建连接与监听器
| import EventSource from "react-native-sse"; |
| |
| const es = new EventSource("https://your-sse-server.com/.well-known/mercure"); |
| |
| es.addEventListener("open", (event) => { |
| console.log("Open SSE connection."); |
| }); |
| |
| es.addEventListener("message", (event) => { |
| console.log("New message event:", event.data); |
| }); |
| |
| es.addEventListener("error", (event) => { |
| if (event.type === "error") { |
| console.error("Connection error:", event.message); |
| } else if (event.type === "exception") { |
| console.error("Error:", event.message, event.error); |
| } |
| }); |
| |
| es.addEventListener("close", (event) => { |
| console.log("Close SSE connection."); |
| }); |
4. 传参以及其他配置
| new EventSource(url: string | URL, options?: EventSourceOptions); |
| const options: EventSourceOptions = { |
| method: 'GET', // 请求方法。默认值: GET |
| timeout: 0, // 在没有任何活动的情况下,连接超时的时间(毫秒)。默认值: 0(无超时) |
| timeoutBeforeConnection: 500, // 在建立初始连接之前等待的时间(毫秒)。默认值: 500 |
| withCredentials: false, // 在跨站点的Access-Control请求中包含凭据。默认值: false |
| headers: {}, // 请求头信息。默认值: {} |
| body: undefined, // 在连接时发送的请求体。默认值: undefined |
| debug: false, // 显示用于调试目的的 console.debug 消息。默认值: false |
| pollingInterval: 5000, // 重连之间的时间间隔(毫秒)。如果设置为 0,则禁用重连。默认值: 5000 |
| lineEndingCharacter: null // 用于表示接收数据中行结尾的字符。常见值:'\n' 表示 LF(Unix/Linux),'\r\n' 表示 CRLF(Windows),'\r' 表示 CR(旧版Mac)。默认值: null(从事件中自动检测) |
| } |
5. 完整示例
点击查看代码
| import { useEffect, useState } from "react"; |
| import { Text, View } from "react-native"; |
| import EventSource from "react-native-sse"; |
| |
| const OpenAIToken = '[Your OpenAI token]'; |
| |
| export default function App() { |
| const [text, setText] = useState<string>("Loading..."); |
| |
| useEffect(() => { |
| const es = new EventSource( |
| "https://api.openai.com/v1/chat/completions", |
| { |
| headers: { |
| "Content-Type": "application/json", |
| Authorization: `Bearer ${OpenAIToken}`, |
| }, |
| method: "POST", |
| |
| body: JSON.stringify({ |
| model: "gpt-3.5-turbo-0125", |
| messages: [ |
| { |
| role: "system", |
| content: "You are a helpful assistant.", |
| }, |
| { |
| role: "user", |
| content: "What is the meaning of life?", |
| }, |
| ], |
| max_tokens: 600, |
| n: 1, |
| temperature: 0.7, |
| stream: true, |
| }), |
| pollingInterval: 0, |
| } |
| ); |
| |
| es.addEventListener("open", () => { |
| setText(""); |
| }); |
| |
| es.addEventListener("message", (event) => { |
| if (event.data !== "[DONE]") { |
| const data = JSON.parse(event.data); |
| |
| if (data.choices[0].delta.content !== undefined) { |
| setText((text) => text + data.choices[0].delta.content); |
| } |
| } |
| }); |
| |
| return () => { |
| es.removeAllEventListeners(); |
| es.close(); |
| }; |
| }, []); |
| |
| return ( |
| <View> |
| <Text>{text}</Text> |
| </View> |
| ); |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程