{"name":"tcp-base","version":"3.2.0","description":"A base class for tcp client with basic functions","main":"lib/base.js","scripts":{"lint":"eslint --ext .js .","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":">= 6.0.0"},"devDependencies":{"egg-bin":"^5.2.0","eslint":"^8.23.0","eslint-config-egg":"^12.0.0","mm":"^2.1.0","mz-modules":"^1.0.0","pedding":"^1.1.0"},"dependencies":{"is-type-of":"^1.0.0","sdk-base":"^3.1.1"},"gitHead":"a13f7c099358af78073bc9e75484aa7bd7486e95","_id":"tcp-base@3.2.0","_nodeVersion":"18.17.1","_npmVersion":"9.6.7","dist":{"integrity":"sha512-fFAqH8QTbheuEbXLdbxTSe31Gkw6Lg3nq4loyrxIXM6+ILGdbYXEblgyuu7UltOkOHbP/q2iqaC+gIXXu0C5bg==","shasum":"bc83618edb26bb17bb9b082216ec242ec9137480","tarball":"http://registry-npm.ywwl.com/tcp-base/download/tcp-base-3.2.0.tgz","fileCount":4,"unpackedSize":16437,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIG+8Zz90E5YCDJVFfT2lh9OGa4gge4/5zAQLZ+EsN4f2AiEArpNo/pbf/mi7eIAOKV6YW+o5HummfCSP89+AkxhsEb8="}],"size":5420,"noattachment":false,"key":"/tcp-base/-/tcp-base-3.2.0.tgz"},"_npmUser":{"name":"fengmk2","email":"fengmk2@gmail.com"},"directories":{},"maintainers":[{"name":"coderhaoxin","email":"haoxinst@gmail.com"},{"name":"gxcsoccer","email":"gxcsoccer@gmail.com"},{"name":"shaoshuai0102","email":"shaoshuai0102@gmail.com"},{"name":"fengmk2","email":"fengmk2@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/tcp-base_3.2.0_1693404708851_0.16258012876931227"},"_hasShrinkwrap":false,"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":1693404709006,"_cnpm_publish_time":1693404709006,"dist-tags":{"latest":"3.2.0"}}