diff --git a/websocket-starter/README.md b/websocket-starter/README.md new file mode 100644 index 0000000..cbe9fc3 --- /dev/null +++ b/websocket-starter/README.md @@ -0,0 +1,70 @@ +# 快速构建 websocket-starter + +**中文** | [English](./README_EN.md) + +## 简介 + +websocket-starter 模板使用 Tencent SCF 组件及其触发器能力,方便的在腾讯云创建,配置和管理一个 websocket-starter 应用。 + +## 快速开始 + +### 1. 安装 + +```bash +# 安装 Serverless Framework +npm install -g serverless +``` + +### 2. 创建 + +通过如下命令直接下载该例子: + +```bash +serverless init websocket-starter --name example +cd example +``` + +### 3. 部署 + +在 `serverless.yml` 文件所在的项目根目录,运行以下指令,将会弹出二维码,直接扫码授权进行部署: + +```bash +serverless deploy +``` + +> **说明**:如果鉴权失败,请参考 [权限配置](https://cloud.tencent.com/document/product/1154/43006) 进行授权。 + +### 4. 查看状态 + +执行以下命令,查看您部署的项目信息: + +```bash +serverless info +``` + +### 5. 移除 + +可以通过以下命令移除 websocket-starter 应用 + +```bash +serverless remove +``` + +### 账号配置(可选) + +serverless 默认支持扫描二维码登录,用户扫描二维码后会自动生成一个 `.env` 文件并将密钥存入其中. +如您希望配置持久的环境变量/秘钥信息,也可以本地创建 `.env` 文件, +把从[API 密钥管理](https://console.cloud.tencent.com/cam/capi)中获取的 `SecretId` 和`SecretKey` 填入其中. + +> 如果没有腾讯云账号,可以在此[注册新账号](https://cloud.tencent.com/register)。 + +```bash +# 腾讯云的配置信息 +touch .env +``` + +``` +# .env file +TENCENT_SECRET_ID=123 +TENCENT_SECRET_KEY=123 +``` diff --git a/websocket-starter/README_EN.md b/websocket-starter/README_EN.md new file mode 100644 index 0000000..94d1cf1 --- /dev/null +++ b/websocket-starter/README_EN.md @@ -0,0 +1,79 @@ +# Quickly create and deploy websocket-starter application + +[中文](./README.md) | **English** + +## Introduction + +Easily deploy websocket-starter applications to Tencent Cloud's serverless infrastructure using this Serverless Framework Component. +Your application will auto-scale, never charge you for idle time, and require little-to-zero administration. + +## Quick Start + +### 1. Install + +```bash +# Install Serverless Framework +npm install -g serverless +``` + +### 2. Initialize + +Initializing the websocket-starter template by running this following command: + +```bash +serverless init websocket-starter --name example +cd example +``` + +### 3. Deploy + +You can use following command to deploy the APP. + +```bash +cd websocket-starter +serverless deploy +``` + +This command will walk you through signing up a Tencent Cloud Account to deploy the APP. + +### 4. Monitor + +Anytime you need to know more about your running websocket instance, you can run `serverless info` to view the most critical info. +This is especially helpful when you want to know the outputs of your instances so that you can reference them in another instance. +You will also see a url where you'll be able to view more info about your instance on the Serverless Dashboard. + +It also shows you the status of your instance, when it was last deployed, and how many times it was deployed. +To dig even deeper, you can pass the --debug flag to view the state of your component instance in case the deployment failed for any reason. + +```bash +serverless info +``` + +### 5. Remove + +If you wanna tear down your entire infrastructure that was created during deployment, +just run `serverless remove` and serverless will remove all the data it needs from the built-in state storage system to delete only the relevant cloud resources that it created. + +```bash +serverless remove +``` + +### Setting up credentials (Optional) + +By default, you are able to login your Tencent Cloud account by scanning QR code and an `.env` file with credentials is auto generated. +The credentials will be expired after 2 hours. +If you would like to use persistent credentials, +you can [create an API Key here](https://console.cloud.tencent.com/cam/capi) and add the `SecretId` and `SecretKey` into the `.env` file + +> If you don's have a Tencent Cloud Account, you can register [here](https://cloud.tencent.com/register) + +```bash +# Add your Tencent credentials here +touch .env +``` + +``` +# .env file +TENCENT_SECRET_ID=123 +TENCENT_SECRET_KEY=123 +``` diff --git a/websocket-starter/serverless.template.yml b/websocket-starter/serverless.template.yml new file mode 100644 index 0000000..0461992 --- /dev/null +++ b/websocket-starter/serverless.template.yml @@ -0,0 +1,16 @@ +name: websocket-starter +displayName: websocket 项目模版 +author: Tencent Cloud, Inc. +org: Tencent Cloud, Inc. +description: Deploy an websocket app via SCF and Apigateway +description-i18n: + zh-cn: 快速部署一个 websocket 基础应用 +type: template +keywords: tencent, websocket, nodejs, SCF, APIGateway +repo: https://github.com/serverless-components/tencent-examples/tree/master/websocket-starter +readme: https://github.com/serverless-components/tencent-examples/tree/master/websocket-starter/README.md +license: MIT +src: + src: ./src + exclude: + - '**/node_modules' diff --git a/websocket-starter/src/serverless.yml b/websocket-starter/src/serverless.yml new file mode 100644 index 0000000..1a99030 --- /dev/null +++ b/websocket-starter/src/serverless.yml @@ -0,0 +1,18 @@ +component: websocket +app: appDemo +name: websocketDemo + +inputs: + region: ap-guangzhou + src: + src: ./ + exclude: + - .env + faas: + name: websocket-function + timeout: 10 + apigw: + timeout: 30 + environment: release + protocols: + - https diff --git a/websocket-starter/src/sls.js b/websocket-starter/src/sls.js new file mode 100644 index 0000000..c50f9af --- /dev/null +++ b/websocket-starter/src/sls.js @@ -0,0 +1,44 @@ +/** + * Connect + */ +on('connect', async (data, socket) => { + console.log(`connect ${socket.id}`); + console.log('data', data); + return 'connected'; +}); + +/** + * Disconnect + */ +on('disconnect', async (data, socket) => { + console.log(`disconnect ${socket.id}`); + console.log('data', data); + return 'closed'; +}); + +/** + * Message + */ +on('message', async (data, socket) => { + console.log('message', socket, data); + console.log('sending to: ', socket.id); + await socket.send( + JSON.stringify({ status: 'sending data', data: data || 'hello websocket' }), + socket.id, + ); +}); + +/** + * Default + */ +on('default', async (data, socket) => { + console.log('message', socket, data); + console.log('sending to: ', socket.id); + await socket.send( + JSON.stringify({ + status: 'sending default data', + data: data || 'hello websocket', + }), + socket.id, + ); +});