diff --git a/docs/en/guide/interaction/networking.mdx b/docs/en/guide/interaction/networking.mdx index 4c19d2f0a..a78fd1ddb 100644 --- a/docs/en/guide/interaction/networking.mdx +++ b/docs/en/guide/interaction/networking.mdx @@ -131,6 +131,25 @@ convert `string` to `arraybuffer` const arraybuffer = TextCodecHelper.encode(str); ``` +## Using EventSource + +Lynx provides the [EventSource](/api/lynx-api/global/fetch) with the same usage as the Web. + +You can refer to the MDN guides on [Using server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) for more information. + +```typescript +const url = 'https://sse.dev/test'; +const eventSource = new lynx.EventSource(url2); + +eventSource.onmessage = (event) => { + console.log('Received message:', event.data); +}; + +eventSource.onerror = (error) => { + console.error('EventSource failed:', error); +}; +``` + ## Using Other Networking Libraries Since The Fetch API is built into Lynx. This means that you can use third party libraries that depend on it. diff --git a/docs/zh/guide/interaction/networking.mdx b/docs/zh/guide/interaction/networking.mdx index 1764c5dd7..c41979306 100644 --- a/docs/zh/guide/interaction/networking.mdx +++ b/docs/zh/guide/interaction/networking.mdx @@ -128,6 +128,25 @@ const str = TextCodecHelper.decode(arraybuffer); const arraybuffer = TextCodecHelper.encode(str); ``` +## 使用 EventSource + +LynxSDK 提供 Web 标准完全一致的 `EventSource`,以处理 SSE 协议。 + +你可以查阅 MDN 文档的 [Using server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events) 了解如何使用 `EventSource` 及 SEE 协议。 + +```typescript +const url = 'https://sse.dev/test'; +const eventSource = new lynx.EventSource(url2); + +eventSource.onmessage = (event) => { + console.log('Received message:', event.data); +}; + +eventSource.onerror = (error) => { + console.error('EventSource failed:', error); +}; +``` + ## 使用其他网络库 由于 Fetch API 已内置于 Lynx,这意味着您可以使用依赖于该 API 的第三方库。