-
-
Notifications
You must be signed in to change notification settings - Fork 681
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
122 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
::: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
|
||
# 云函数拦截器 | ||
# 拦截器 | ||
|
||
本节介绍云函数拦截器,当请求云函数时会先执行拦截器,可以通过拦截器对云函数的请求加以控制。 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
::: |