All Projects → luckydrq → tcp-net

luckydrq / tcp-net

Licence: MIT license
Build tcp applications in a stable and elegant way

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to tcp-net

EasyFileTransfer
An easy way to transfer file with any size on network with tcp protocol.
Stars: ✭ 30 (-28.57%)
Mutual labels:  socket, tcp, tcp-server, tcp-client
Socketify
Raw TCP and UDP Sockets API on Desktop Browsers
Stars: ✭ 67 (+59.52%)
Mutual labels:  socket, tcp, tcp-server, tcp-client
Oksocket
An blocking socket client for Android applications.
Stars: ✭ 2,359 (+5516.67%)
Mutual labels:  socket, tcp, tcp-server, tcp-client
Tinytcpserver
A small tcp server working under Mono or .NET (4.0) and provides hooks for handling data exchange with clients (works under mono and .net). Behaviour/protocol/reaction could be specified via custom C# script.
Stars: ✭ 14 (-66.67%)
Mutual labels:  socket, tcp, tcp-server, tcp-client
Bizsocket
异步socket,对一些业务场景做了支持
Stars: ✭ 469 (+1016.67%)
Mutual labels:  socket, tcp, tcp-server, tcp-client
Simplenet
An easy-to-use, event-driven, asynchronous network application framework compiled with Java 11.
Stars: ✭ 164 (+290.48%)
Mutual labels:  tcp, tcp-server, tcp-client
cAndroid
cAndroid is tool for control your PC by Android phone
Stars: ✭ 23 (-45.24%)
Mutual labels:  socket, tcp-server, tcp-client
QTcpSocket
A simple Qt client-server TCP architecture to transfer data between peers
Stars: ✭ 62 (+47.62%)
Mutual labels:  tcp, tcp-server, tcp-client
Packetsender
Network utility for sending / receiving TCP, UDP, SSL
Stars: ✭ 1,349 (+3111.9%)
Mutual labels:  tcp, tcp-server, tcp-client
go-eventserver
A socket server which reads events from an event source and forwards them to the user clients when appropriate
Stars: ✭ 18 (-57.14%)
Mutual labels:  socket, tcp, tcp-server
SuperSimpleTcp
Simple wrapper for TCP client and server in C# with SSL support
Stars: ✭ 263 (+526.19%)
Mutual labels:  tcp, tcp-server, tcp-client
Simpletcp
A minimal non-blocking TCP server written for Python 3.
Stars: ✭ 162 (+285.71%)
Mutual labels:  tcp, tcp-server, tcp-client
React Native Tcp Socket
React Native TCP socket API for Android, iOS & macOS with client SSL/TLS support
Stars: ✭ 112 (+166.67%)
Mutual labels:  tcp, tcp-server, tcp-client
tcp server client
A thin and simple C++ TCP client server
Stars: ✭ 124 (+195.24%)
Mutual labels:  tcp, tcp-server, tcp-client
Godsharp.socket
An easy-to-use .NET socket server and client.
Stars: ✭ 35 (-16.67%)
Mutual labels:  socket, tcp, tcp-server
Simpletcp
Simple wrapper for TCP client and server in C# with SSL support
Stars: ✭ 99 (+135.71%)
Mutual labels:  tcp, tcp-server, tcp-client
Simpleunitytcp
🖧 Simple Unity Project to show how TCP communication are builded in C# without multi-threading or Unity network (Unet) involved.
Stars: ✭ 22 (-47.62%)
Mutual labels:  tcp, tcp-server, tcp-client
Easytcp
Simple framework for TCP clients and servers. Focused on performance and usability.
Stars: ✭ 60 (+42.86%)
Mutual labels:  tcp, tcp-server, tcp-client
Chat Socket
A simple chat room using java socket with the client-server paradigm
Stars: ✭ 24 (-42.86%)
Mutual labels:  socket, tcp-server, tcp-client
Deta cache
缓存cache服务器
Stars: ✭ 106 (+152.38%)
Mutual labels:  socket, tcp, tcp-server

@luckydrq/tcp-net

编写稳定可靠的TCP长连接应用。

封装TCP连接的拆解包逻辑,支持Length-BasedLine-Based两种协议格式。

注意,本模块只支持node 8.x以上。

NPM version build status Test coverage David deps Known Vulnerabilities NPM download Gitter

Feature

  • 长连接
  • 连接保持(心跳检测)
  • 支持多请求多响应
  • 可扩展协议
  • 可扩展编解码器

Protocol

我们对一个tcp包协议格式进行了如下约定:

|-----------------------------------------------|
| 1 byte  | 4 bytes  | 1 byte     | N bytes     |
|-----------------------------------------------|
| reqType | packetId | packetType | custom body |
|-----------------------------------------------|
  • reqType: Number, 请求类型。0 - 请求 1 - 响应
  • packetId: Number, 包id。
  • packetType: Number, 包类型。0 - 普通包 1 - 心跳包
  • custom body: 自定义包体。

其中,自定义包体就是用户发送的数据,它可以支持Length-BasedLine-Based两种子协议。

Length-Based Protocol

默认内置。适用于用户定义的包长度是确定的场景。原理很简单,先用一个field标识包体的长度,紧接着就是相应长度的包体。field本身长度支持自定义设置,默认是4字节。

|------------------|---------------|
|      4 bytes     |    N bytes    |
|------------------|---------------|
| PACKET LENGTH(N) | HEADER | BODY |
|------------------|---------------|

Line-Based Protocol

适用于用户定义的包长度不确定的场景。它用一种边界符来表示包的结束。最典型的例子,http协议格式就是Line-Based的协议,它的边界符(或者说分隔符)是CRLF(\r\n)。边界符支持自定义设置,默认是CRLF

|------------------|-----------|
|      N bytes     | delimiter |
|------------------|-----------|
|    PACKET BODY   |    \r\n   |
|------------------|-----------|

Install

$ npm i @luckydrq/tcp-net -S

Examples

Basic

client和server应该使用相同的协议,保持拆解包逻辑一致。

server

const net = require('net');
const { Connection } = require('@luckydrq/tcp-net');

net.createServer(socket => {
  const conn = new Connection({
    socket,
    // protocol: new LengthBasedProtocol(),  default
  });

  // 发送一个packet
  conn.write('hello world');

  // 收到packet
  conn.on('packet', ({ packetId, body }) => {
    console.log(packetId); // 1
    console.log(JSON.parse(body)); // { name: 'xuezu' }
  });
}).listen();

client

const { Connection } = require('@luckydrq/tcp-net');
const conn = new Connection({ host, port });

// 发送一个packet
conn.write({ name: 'xuezu' });

// 收到一个packet
conn.on('packet', ({ packetId, body }) => {
  console.log(packetId); // 1
  console.log(body); // hello world
});

Heartbeat

内置了心跳检测,默认每隔5秒发起一次心跳检测,响应超时时间为3秒,超时3次则关闭连接。支持配置,在构造函数中传入:

const conn = new Connection({
  heartbeatInterval: 5 * 1000, // 心跳间隔
  heartbeatTimeout: 3 * 1000, // 超时时间
  heartbeatLimit: 3, // 允许超时次数
});

conn.on('heartbeatTimeout', () => {
  console.log('连接已经关闭');
});

Transcoder

通常需要对数据做编解码,本例子展示如何接入一个自定义编解码器。如果不提供,默认只做基本的序列化

自定义实现一个transCoder,只需要实现.encode.decode方法即可,也可以只实现其中之一,不过通常编解码逻辑都是对应的。

server

const transCoder = {
  encode(buf) {
    // 调用hessian编码
    // 注意,需要返回Buffer对象
    return hessian.encode(buf);
  },

  decode(data) {
    // 可以返回任意类型的数据
    return hessian.decode(buf);
  },
};

net.createServer(socket => {
  const conn = new Connection({
    socket,
    protocol: new LengthBasedProtocol({ transCoder }), // 在协议中传入transCoder
  });

  // 收到packet
  conn.on('packet', ({ body }) => console.log(body));
}).listen();

client

const conn = new Connection({
  host,
  port,
  protocol: new LengthBasedProtocol({ transCoder }),
});
// 发一个packet
conn.write({ name: 'xuezu' });

更多例子

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].