Skip to content

Commit

Permalink
docs: add websocket docs
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Jan 2, 2024
1 parent 64fa5a5 commit 9236d65
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 4 deletions.
5 changes: 2 additions & 3 deletions docs/.vitepress/zh.mts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ export const SidebarItemsInZh: DefaultTheme.SidebarItem[] = [
collapsed: true,
items: [
{ text: '拦截器', link: '/zh/cloud-function/interceptor' },
{ text: '初始化(TBD)' },
{ text: 'NotFound(TBD)' },
{ text: 'WebSocket(TBD)' },
{ text: '初始化', link: '/zh/cloud-function/init' },
{ text: 'WebSocket', link: '/zh/cloud-function/websocket' },
],
},
]
Expand Down
49 changes: 49 additions & 0 deletions docs/zh/cloud-function/init.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

# 用于应用初始化的云函数

本节介绍一个特殊的云函数,它会在应用启动时自动执行一次。

::: info 本节目录
[[toc]]
:::


## 创建初始化云函数

创建一个云函数并且命名为 `__init__` 即可,当应用实例启动时,会自动执行该云函数。

::: tip
- 应用启动、重启皆会触发该云函数的执行。
- 若应用有多个实例,则启动时会触发多次执行,即每个应用实例启动时都会触发初始化函数的执行。
:::

下面的代码展示在应用启动时初始化一个管理员:

```typescript
import cloud from '@lafjs/cloud'

export default async function (ctx: FunctionContext) {
const db = cloud.mongo.db
const admin = await db.collection('admin').findOne()

if(!admin) {
await db.collection('admin').insertOne({
username: 'admin',
password: 'abc123',
createdAt: new Date()
})
}
}
```

## 下一步
::: tip
- [HTTP 请求](./request.md)
- [HTTP 响应](./response.md)
- [HTTP 认证](./auth.md)
- [处理文件上传](./files.md)
- [发起网络请求](./fetch.md)
- [云数据库](../cloud-database/index.md)
- [云存储](../cloud-storage/index.md)
:::

2 changes: 1 addition & 1 deletion docs/zh/cloud-function/interceptor.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# 云函数拦截器
# 拦截器

本节介绍云函数拦截器,当请求云函数时会先执行拦截器,可以通过拦截器对云函数的请求加以控制。

Expand Down
70 changes: 70 additions & 0 deletions docs/zh/cloud-function/websocket.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

# 云函数处理 WebSocket 连接

本节介绍使用云函数处理 WebSocket 连接。

## 创建 WebSocket 云函数

创建一个固定命名为 `__websocket__` 的云函数,该云函数专用于处理 WebSocket 连接。

::: tip
WebSocket 云函数名为固定云函数名:__websocket__,其他名称均不会生效
:::

以下是处理 WebSocket 的示例代码:

```typescript
export async function main(ctx: FunctionContext) {

if (ctx.method === "WebSocket:connection") {
ctx.socket.send('hi connection succeed')
}

if (ctx.method === 'WebSocket:message') {
const { data } = ctx.params
console.log(data.toString())
ctx.socket.send("I have received your message");
}

if (ctx.method === 'WebSocket:error') {
const error = ctx.params
console.log(error)
}

if (ctx.method === 'WebSocket:close') {
const { code, reason } = ctx.params
console.log(code, reason)
}
}
```

### 客户端连接

```typescript
const wss = new WebSocket("wss://your-own-appid.laf.run")

wss.onopen = (socket) => {
console.log("connected")
wss.send("hi")
};

wss.onmessage = (res) => {
console.log("收到了新的信息......");
console.log(res.data);
};

wss.onclose = () => {
console.log("closed");
}
```

## 下一步
::: tip
- [HTTP 请求](./request.md)
- [HTTP 响应](./response.md)
- [HTTP 认证](./auth.md)
- [处理文件上传](./files.md)
- [发起网络请求](./fetch.md)
- [云数据库](../cloud-database/index.md)
- [云存储](../cloud-storage/index.md)
:::

0 comments on commit 9236d65

Please sign in to comment.