Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/en/guide/interaction/networking.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 19 additions & 0 deletions docs/zh/guide/interaction/networking.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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 的第三方库。
Expand Down