coa-tcp
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

coa-tcp

GitHub license npm version npm downloads PRs Welcome

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

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

特性

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

快速开始

安装

yarn add coa-tcp

快速开始

import { CoaClientPool, CoaTcp } from 'coa-tcp'

// 创建一个客户端连接池
const clientPool = new CoaClientPool()

// 创建一个tcp服务
const tcpServer = new CoaTcp(clientPool, 5000)

// 启动服务
tcpServer.start()

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

[TCP] Listening on port 5000

自定义客户端

import { Socket } from 'net'
import { CoaClient, CoaClientPool, CoaTcp } from 'coa-tcp'

// 自定义客户端
class CustomClient extends CoaClient {
  // 接收到数据
  async onData(raw: Buffer) {
    // 收到数据后要处理的事情
  }

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

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

// 自定义客户端连接池
class CustomClientPool extends CoaClientPool<CustomClient> {
  // 生成一个自定义的客户端
  newClient(socket: Socket) {
    return new CustomClient(
      socket,
      `custom-id-${++this.increment}`,
      'CustomClient'
    )
  }
}

// 创建一个客户端连接池
const clientPool = new CustomClientPool()

// 创建一个tcp服务
const tcpServer = new CoaTcp(clientPool, 5000)

// 启动服务
tcpServer.start()

Dependents (0)

Package Sidebar

Install

npm i coa-tcp

Weekly Downloads

1

Version

1.0.2

License

MIT

Unpacked Size

11.8 kB

Total Files

14

Last publish

Collaborators

  • adaex