node-socket:Node端基于TLV二进制包格式进行封装的tcp socket库,包括粘包断包处理
发布于 1 年前 作者 xiaoping6688 2897 次浏览 来自 分享

node-socket

Based on node.js tcp socket of TLV packet structure (Node端基于TLV二进制协议格式进行封装的socket工具库,包括粘包断包处理)

npm package NPM downloads Percentage of issues still open

其他相关

  • mqtt-socket:Eclipse Paho MQTT Client 封装,实现重连、队列和日志功能等
  • easemob-webim-socket:环信WEBIM实时通信封装 + 客户端模拟器(electron)

Install

$ npm install --save node-socket

Usage

// for client
const socket = require('node-socket').client
socket.connect(options, onConnected, onReceived)

function onReceived (tag, value) {
  switch (tag){
    case TAG_FROM_REGIST_SUCCESS:

      // send message to server
      socket.send(1, { token: '123' })

      break;
    case TAG_FROM_REGIST_FAIL:

      break;
  }
}


// for server
const server = require('node-socket').server

var options = {
  port: 11000,
  timeout: 15000,
  heartbeatTag: 0,
  heartbeatInterval: 7000,
  recreateInterval: 1000
}

server.listen(options, onClientConnected, onClientDisconncted, onClientReceived)

function onClientReceived (tag, value, from) {
  // server.broadcast(tag, value) // all
  server.broadcast(tag, value, null, from) // all, except from
  // server.broadcast(tag, value, clientList, from);
}

//=> @see test_server.js
//=> @see test_client.js
回到顶部