{"name":"tcp-base","version":"1.1.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 -- --fix && 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, 7"},"devDependencies":{"egg-bin":"1","egg-ci":"1","eslint":"3","eslint-config-egg":"3","mm":"2","npminstall":"2","pedding":"1","should":"11"},"dependencies":{"is-type-of":"^1.0.0","sdk-base":"^2.0.1"},"gitHead":"74cb511afc10a196104ce4789811af9e1b681c1f","_id":"tcp-base@1.1.0","_shasum":"5b2dcffbc51fb3c3add05c298388be34e6470cd4","_from":".","_npmVersion":"3.10.8","_nodeVersion":"6.9.1","_npmUser":{"name":"fengmk2","email":"fengmk2@gmail.com"},"dist":{"shasum":"5b2dcffbc51fb3c3add05c298388be34e6470cd4","tarball":"http://registry-npm.ywwl.com/tcp-base/download/tcp-base-1.1.0.tgz","integrity":"sha512-rc1C6Ux/EAv4SyoLdScVPgdJmkodkYO06YrGZPkTcV8PIoB98rPa1BEUiWTC+zNh5NtvOZtsQz+RJ3BDi37oGg==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDdMnqkajepPT3xSaYW6h70grpj3TK8w7FXmQoJ5Zw/2gIgf1bElOFX+WLVLpSpYJhPqI2a0gTTt6PXaamy/s7cJ0U="}],"size":5593,"noattachment":false,"key":"/tcp-base/-/tcp-base-1.1.0.tgz"},"maintainers":[{"name":"coderhaoxin","email":"coderhaoxin@outlook.com"},{"name":"fengmk2","email":"fengmk2@gmail.com"},{"name":"gxcsoccer","email":"gxcsoccer@126.com"}],"_npmOperationalInternal":{"host":"packages-18-east.internal.npmjs.com","tmp":"tmp/tcp-base-1.1.0.tgz_1480475684826_0.09407295333221555"},"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":1480475685449,"_cnpm_publish_time":1480475685449,"dist-tags":{"latest":"3.2.0"}}