Git Product home page Git Product logo

xiaoping6688 / node-socket Goto Github PK

View Code? Open in Web Editor NEW
7.0 3.0 2.0 17 KB

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

Home Page: https://www.npmjs.com/package/node-socket

License: MIT License

JavaScript 100.00%
tcp-socket tlv nodejs-socket js-tlv tlv-socket js-tlv-socket nodejs-tlv-socket node-tcp-socket socket-client

node-socket's Introduction

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

node-socket's People

Contributors

xiaoping6688 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

node-socket's Issues

ExBuffer 环形队列读写起始位置

如果缓存长度30字节,位置为 0 到 29,已有未读数据10字节位于 10 到 19,则 _readOffset = 10,_putOffset = 20,此时新增数据20字节,队列尾部存10字节,队列头部存10字节,此时 _putOffset = 10,将与 _readOffset 重合?

只要 新增数据长度 + 有效数据长度 = 缓存长度,且缓存尾容量不足以放下整个新增数据,需要部分或全部放到缓存头,就会导致重合?

建议将:
//当前缓冲区已经不能满足次数数据了
if (len + getLen() > _buffer.length) {
var ex = Math.ceil((len + getLen()) / 1024); //每次扩展1kb
var tmp = Buffer.alloc(ex * 1024);

修改为:
//当前缓冲区已经不能满足次数数据了
if (len + getLen() >= _buffer.length) {
var ex = Math.ceil((len + getLen()) / 1024); //每次扩展1kb
var tmp = Buffer.alloc(ex * 1024 + 1);
1、相等时就扩充缓存
2、多一字节以避免 (len + getLen()) / 1024 刚好为整数使得新旧缓存大小一样

需要考虑 1024 + 1 字节可能导致 内存 或 CPU Cache 未对齐?
要不就直接 Buffer.alloc(ex * 1024 + 1024); 算了。

另外,直接迁移有用数据到新缓存,而不是先拷贝整个老缓存到新缓存再整理,是否能提高效率?

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.