Skip to content

Commit

Permalink
Prettier format
Browse files Browse the repository at this point in the history
  • Loading branch information
adaex committed Jun 10, 2021
1 parent 3391da4 commit 659e13b
Show file tree
Hide file tree
Showing 18 changed files with 1,687 additions and 136 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
23 changes: 23 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"env": {
"es2021": true,
"mocha": true,
"node": true
},
"extends": ["standard-with-typescript", "prettier"],
"parserOptions": {
"ecmaVersion": 2021,
"project": "tsconfig.json",
"sourceType": "module"
},
"plugins": ["@typescript-eslint"],
"rules": {
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/naming-convention": "off",
"@typescript-eslint/no-misused-promises": "off",
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/restrict-plus-operands": "off",
"@typescript-eslint/restrict-template-expressions": "off",
"@typescript-eslint/strict-boolean-expressions": "off"
}
}
11 changes: 2 additions & 9 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Build

on:
push:
branches:
- '**'
pull_request:
branches:
- '**'

jobs:

build-check:

name: Build check

runs-on: ubuntu-latest
Expand All @@ -23,4 +15,5 @@ jobs:
- uses: actions/checkout@v2

- run: yarn
- run: yarn build
- run: yarn lint
- run: yarn build
29 changes: 15 additions & 14 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages

name: Release

on:
release:
types:
- created
- published

jobs:

publish-npm:

name: NPM Publish
publish:
name: Publish

if: github.repository_owner == 'coajs'

Expand All @@ -26,20 +21,26 @@ jobs:
node-version: 12
registry-url: https://registry.npmjs.org

- name: Build
- name: Yarn
run: |
yarn
npm --no-git-tag-version version ${{ github.event.release.tag_name }}
npm run build
- name: Lint
run: |
yarn lint
- name: Build
run: |
yarn version --no-git-tag-version --new-version ${{ github.event.release.tag_name }}
yarn build
- name: Publish
env:
NODE_AUTH_TOKEN: ${{ secrets.AEX_NPM_TOKEN }}
run: |
cd dist
npm publish
yarn publish dist
- name: Sync
run: |
sleep 5s
npm run sync
yarn sync
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
5 changes: 5 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"printWidth": 160,
"semi": false,
"singleQuote": true
}
22 changes: 10 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
[![npm downloads](https://img.shields.io/npm/dm/coa-tcp.svg?style=flat-square)](http://npm-stat.com/charts.html?package=coa-tcp)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://github.com/coajs/coa-tcp/pulls)

一个轻量的TCP服务框架,是COA核心库组成之一
一个轻量的 TCP 服务框架,是 COA 核心库组成之一

可以快速创建一个TCP服务,支持单片机或物联网设备、跨平台TCP应用的连接
可以快速创建一个 TCP 服务,支持单片机或物联网设备、跨平台 TCP 应用的连接

## 特性

- **简单轻量** 基于 `Node.js` 内置的 [Net](https://nodejs.org/api/net.html) 模块,简单轻量,不依赖于其他第三方库
- **自动管理连接池** 自动维护管理客户端连接池,无需关心连接和释放的问题,专注于收发消息和业务逻辑的开发
- **TypeScript** 全部使用TypeScript书写,类型约束、IDE友好
- **TypeScript** 全部使用 TypeScript 书写,类型约束、IDE 友好

## 快速开始

Expand All @@ -38,7 +38,7 @@ const tcpServer = new CoaTcp(clientPool, 5000)
tcpServer.start()
```

当控制台输出类似提示,则说明TCP服务已经正常启动,可以接受客户端的连接了
当控制台输出类似提示,则说明 TCP 服务已经正常启动,可以接受客户端的连接了

```shell
[TCP] Listening on port 5000
Expand All @@ -52,30 +52,28 @@ import { CoaClient, CoaClientPool, CoaTcp } from 'coa-tcp'

// 自定义客户端
class CustomClient extends CoaClient {

// 接收到数据
async onData (raw: Buffer) {
async onData(raw: Buffer) {
// 收到数据后要处理的事情
}

// 上线
async onOnline (deviceId: string) {
async onOnline(deviceId: string) {
super.onOnline(deviceId)
// 客户端上线要处理的事情
}

// 下线
async onOffline (deviceId: string) {
async onOffline(deviceId: string) {
super.onOffline(deviceId)
// 客户端下线要处理的事情
}
}

// 自定义客户端连接池
class CustomClientPool extends CoaClientPool<CustomClient>{

class CustomClientPool extends CoaClientPool<CustomClient> {
// 生成一个自定义的客户端
newClient (socket: Socket) {
newClient(socket: Socket) {
return new CustomClient(socket, `custom-id-${++this.increment}`, 'CustomClient')
}
}
Expand All @@ -88,4 +86,4 @@ const tcpServer = new CoaTcp(clientPool, 5000)

// 启动服务
tcpServer.start()
```
```
23 changes: 16 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,25 @@
"scripts": {
"dev": "tsc -w",
"build": "rm -rf dist && tsc && cp package.json *.md dist && rm -rf dist/test",
"sync": "curl -X PUT 'https://npm.taobao.org/sync/coa-tcp?sync_upstream=true'",
"publish-prerelease": "yarn build && yarn version --prerelease && cp package.json *.md dist && cd dist && rm -rf test && npm publish",
"test": "NODE_PATH=run node dist/test"
"lint": "eslint .",
"prettier": "prettier -w .",
"test": "NODE_PATH=run node dist/test",
"sync": "curl -X PUT 'https://npm.taobao.org/sync/coa-tcp?sync_upstream=true'"
},
"dependencies": {
"coa-echo": "^1.0.8",
"coa-helper": "^1.1.1"
"coa-echo": "^1.1.1",
"coa-helper": "^1.2.1"
},
"devDependencies": {
"@types/node": "^14.14.37",
"typescript": "^4.2.4"
"@types/node": "^15.12.2",
"@typescript-eslint/eslint-plugin": "^4.26.1",
"eslint": "^7.28.0",
"eslint-config-prettier": "^8.3.0",
"eslint-config-standard-with-typescript": "^20.0.0",
"eslint-plugin-import": "^2.23.4",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^5.1.0",
"prettier": "^2.3.1",
"typescript": "^4.3.2"
}
}
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { CoaClient } from './service/CoaClient'
export { CoaClientPool } from './service/CoaClientPool'
export { CoaTcp } from './service/CoaTcp'

22 changes: 8 additions & 14 deletions src/service/CoaApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,38 @@ import { CoaClient } from './CoaClient'
import { CoaClientPool } from './CoaClientPool'

export class CoaApplication<T extends CoaClient> {

private readonly listenPort: number
private readonly clientPool: CoaClientPool<T>
private readonly tcpServer: Server

constructor (clientPool: CoaClientPool<T>, listenPort: number) {
constructor(clientPool: CoaClientPool<T>, listenPort: number) {
this.clientPool = clientPool
this.listenPort = listenPort
this.tcpServer = createServer(socket => this.connectionListener(socket))
this.tcpServer = createServer((socket) => this.connectionListener(socket))
}

// 监听
start () {
start() {
this.tcpServer.listen(this.listenPort, () => {
echo.cyan('[TCP] Listening on port ' + this.listenPort)
})
}

// 监听连接事件
private connectionListener (socket: Socket) {
private connectionListener(socket: Socket) {
let client: T
// 只有首次收到数据,才开始初始化一个客户端
socket.on('data', async raw => {
if (!client)
client = await this.clientPool.connect(socket)
socket.on('data', async (raw) => {
if (!client) client = await this.clientPool.connect(socket)
await client.onData(raw)
})
// 关闭连接的时候销毁数据
socket.on('close', async () => {
if (client)
await this.clientPool.close(client)
if (client) await this.clientPool.close(client)
})
socket.on('error', _.noop)
// 如果超过30秒没有任何响应,则断开连接
socket.on('timeout', () => socket.end(() => socket.destroy()))
socket.setTimeout(30 * 1000)
}



}
}
24 changes: 8 additions & 16 deletions src/service/CoaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { _ } from 'coa-helper'
import { Socket } from 'net'

export class CoaClient {

socket: Socket
clientId: string
deviceId: string
Expand All @@ -12,7 +11,7 @@ export class CoaClient {
isWorking: boolean
live: number

constructor (socket: Socket, clientId: string, type: string) {
constructor(socket: Socket, clientId: string, type: string) {
this.socket = socket
this.type = type
this.clientId = clientId
Expand All @@ -23,29 +22,22 @@ export class CoaClient {
}

// 连接
async onConnect () {
}
async onConnect() {}

// 连接关闭
async onClose () {
}
async onClose() {}

// 设备上线
async onOnline (deviceId: string) {
}
async onOnline(deviceId: string) {}

// 设备下线
async onOffline (deviceId: string) {
}
async onOffline(deviceId: string) {}

// 接收到数据
async onData (raw: Buffer) {

}
async onData(raw: Buffer) {}

// 设置设备状态
async setDevice ({ deviceId }: { deviceId: string }) {

async setDevice({ deviceId }: { deviceId: string }) {
// 记录上次的设备ID
const lastDeviceId = this.deviceId + ''

Expand All @@ -65,4 +57,4 @@ export class CoaClient {
}
return this.live
}
}
}
Loading

0 comments on commit 659e13b

Please sign in to comment.