{"name":"tcp-base","version":"1.0.0","description":"A base class for tcp client with basic functions","main":"lib/base.js","files":["lib"],"scripts":{"lint":"eslint --ext .js lib test example","test":"npm run lint && npm run test-local","test-local":"egg-bin test","cov":"egg-bin cov","ci":"npm run lint && npm run cov"},"repository":{"type":"git","url":"git+https://github.com/node-modules/tcp-base.git"},"keywords":["tcp"],"author":{"name":"gxcsoccer","email":"gxcsoccer@126.com"},"license":"MIT","bugs":{"url":"https://github.com/node-modules/tcp-base/issues"},"homepage":"https://github.com/node-modules/tcp-base#readme","engines":{"node":">= 4.0.0"},"ci":{"version":"4, 6"},"devDependencies":{"egg-bin":"^1.3.0","egg-ci":"^1.0.3","eslint":"^3.5.0","eslint-config-egg":"^3.1.0","mm":"^2.0.0","npminstall":"^2.2.2","pedding":"^1.0.0","should":"^11.1.0"},"dependencies":{"is-type-of":"^1.0.0","sdk-base":"^2.0.1"},"gitHead":"083fcafc32ad422a78d3970f0d0f58eed9f0257e","_id":"tcp-base@1.0.0","_shasum":"d71cb44ea6959c516be7a5aebe012753e93dad50","_from":".","_npmVersion":"2.15.9","_nodeVersion":"4.5.0","_npmUser":{"name":"gxcsoccer","email":"gxcsoccer@126.com"},"maintainers":[{"name":"gxcsoccer","email":"gxcsoccer@126.com"}],"dist":{"shasum":"d71cb44ea6959c516be7a5aebe012753e93dad50","tarball":"http://registry-npm.ywwl.com/tcp-base/download/tcp-base-1.0.0.tgz","integrity":"sha512-4glsGBY3vDepmQv2cCif6QIJhNsYR6ZU90PeFhkSqy4bfzX6J/ziP3wY+SmGSM8GtMopjNxaahT0FT9tcp4eUQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQD/mBplDcZtNyT/2N7FIgrOvBoZlKICJqJo7PAabHp/yQIgUxacZse0QrW86fYxCTZ/L+ZawNxTp2cCtzrhlLtBisw="}],"size":5202,"noattachment":false,"key":"/tcp-base/-/tcp-base-1.0.0.tgz"},"_npmOperationalInternal":{"host":"packages-16-east.internal.npmjs.com","tmp":"tmp/tcp-base-1.0.0.tgz_1474174335949_0.544915450969711"},"directories":{},"readme":"# tcp-base\n\nA base class for tcp client with basic functions\n\n[![NPM version][npm-image]][npm-url]\n[![Node.js CI](https://github.com/node-modules/tcp-base/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/tcp-base/actions/workflows/nodejs.yml)\n[![Test coverage][codecov-image]][codecov-url]\n[![npm download][download-image]][download-url]\n\n[npm-image]: https://img.shields.io/npm/v/tcp-base.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/tcp-base\n[codecov-image]: https://codecov.io/gh/node-modules/tcp-base/branch/master/graph/badge.svg\n[codecov-url]: https://codecov.io/gh/node-modules/tcp-base\n[download-image]: https://img.shields.io/npm/dm/tcp-base.svg?style=flat-square\n[download-url]: https://npmjs.org/package/tcp-base\n\n## Install\n\n```bash\nnpm install tcp-base --save\n```\n\nNode.js >= 6.0.0 required\n\n## Usage\n\nA quick guide to implement a tcp echo client\n\nClient:\n\n```js\nconst TCPBase = require('tcp-base');\n\n/**\n * A Simple Protocol:\n *   (4B): request id\n *   (4B): body length\n *   ------------------------------\n *   body data\n */\nclass Client extends TCPBase {\n  getHeader() {\n    return this.read(8);\n  }\n\n  getBodyLength(header) {\n    return header.readInt32BE(4);\n  }\n\n  decode(body, header) {\n    return {\n      id: header.readInt32BE(0),\n      data: body,\n    };\n  }\n\n  // heartbeat packet\n  get heartBeatPacket() {\n    return Buffer.from([ 255, 255, 255, 255, 0, 0, 0, 0 ]);\n  }\n}\n\nconst client = new Client({\n  host: '127.0.0.1',\n  port: 8080,\n});\n\nconst body = Buffer.from('hello');\nconst data = Buffer.alloc(8 + body.length);\ndata.writeInt32BE(1, 0);\ndata.writeInt32BE(body.length, 4);\nbody.copy(data, 8, 0);\n\nclient.send({\n  id: 1,\n  data,\n  timeout: 5000,\n}, (err, res) => {\n  if (err) {\n    console.error(err);\n  }\n  console.log(res.toString()); // should echo 'hello'\n});\n```\n\nServer:\n\n```js\n'use strict';\n\nconst net = require('net');\n\nconst server = net.createServer(socket => {\n  let header;\n  let bodyLen;\n\n  function readPacket() {\n    if (bodyLen == null) {\n      header = socket.read(8);\n      if (!header) {\n        return false;\n      }\n      bodyLen = header.readInt32BE(4);\n    }\n\n    if (bodyLen === 0) {\n      socket.write(header);\n    } else {\n      const body = socket.read(bodyLen);\n      if (!body) {\n        return false;\n      }\n      socket.write(Buffer.concat([ header, body ]));\n    }\n    bodyLen = null;\n    return true;\n  }\n\n  socket.on('readable', () => {\n    try {\n      let remaining = false;\n      do {\n        remaining = readPacket();\n      }\n      while (remaining);\n    } catch (err) {\n      console.error(err);\n    }\n  });\n});\nserver.listen(8080);\n```\n\n[MIT](LICENSE)\n","publish_time":1474174337842,"_cnpm_publish_time":1474174337842,"dist-tags":{"latest":"3.2.0"}}