Giter Club home page Giter Club logo

coa-tcp's Introduction

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()

coa-tcp's People

Contributors

adaex avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.