Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

node-socket:Node端基于TLV二进制包格式进行封装的tcp socket库,包括粘包断包处理

$
0
0

node-socket

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

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

欢迎点赞 👏👍😄


Viewing all articles
Browse latest Browse all 14821

Trending Articles