Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add websocket starter #48

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
70 changes: 70 additions & 0 deletions websocket-starter/README.md
Original file line number Diff line number Diff line change
@@ -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
```
79 changes: 79 additions & 0 deletions websocket-starter/README_EN.md
Original file line number Diff line number Diff line change
@@ -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
```
16 changes: 16 additions & 0 deletions websocket-starter/serverless.template.yml
Original file line number Diff line number Diff line change
@@ -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'
18 changes: 18 additions & 0 deletions websocket-starter/src/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
component: websocket
app: appDemo
name: websocketDemo

inputs:
region: ap-guangzhou
src:
src: ./
exclude:
- .env
faas:
Copy link
Contributor

@timqian timqian Nov 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tinafangkunding @AprilJC 基于刚才会上的讨论,你们觉得这边怎么做比较好?

Related issue: https://github.com/serverless/roadmap-tencent/issues/583

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

websocket 是新组件,不涉及到兼容问题

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

考虑到各组件配置方式的一致性,我觉得可能各个组件具有类似的配置模式可能好一些

name: websocket-function
timeout: 10
apigw:
timeout: 30
environment: release
protocols:
- https
44 changes: 44 additions & 0 deletions websocket-starter/src/sls.js
Original file line number Diff line number Diff line change
@@ -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,
);
});