{"name":"tcp-base","version":"3.1.1","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","contributors":"contributors"},"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"},"ci":{"version":"14, 16, 18","os":"linux"},"devDependencies":{"contributors":"^0.5.1","egg-bin":"^5.2.0","egg-ci":"^2.1.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":"d9d7084593b461db7335062d41dbc200e82df78c","_id":"tcp-base@3.1.1","_nodeVersion":"16.17.0","_npmVersion":"6.14.12","dist":{"integrity":"sha512-6wS2yvxq7xqhlpjHPLCYsuy6hIu1HFP3vDqfa1swjQ14MHIz2TW32CvY3hOrt6eM0tmUmy+1rsy52JKqD9hkjQ==","shasum":"7daf9599b148919bc12fc3df14f49b89b748cca1","tarball":"http://registry-npm.ywwl.com/tcp-base/download/tcp-base-3.1.1.tgz","fileCount":5,"unpackedSize":17833,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDwkB8/cJG3zF5Z0cqywTwe9L6SQIxcPtyGSKDsmnMm+QIge5d+Y94+raX8jeNOXxJchX9Gros7gc/F1IlQfBfLdSo="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjEMf2ACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmpQCxAAi/KpJCcKVOJTRKf0oPwl3Mavf70i/7gjqIe5XQ0BcDKZjGnP\r\nwQMjOryyfBeFGAYH2goxVprjYNCziC0SIy8+zIe6rZUj24nwUaStJ9txVjbJ\r\ndBi0zC0Kc3nID5uInj37zmmW629c9WmLma2jCQwDWNBO0e90d75w9WDOHorX\r\nNjx6d5Y2G33eJI3La8wAuIziAbermwLlNFZYSpNPWRRFb2kUbbDGNzCJTW0s\r\nVRPhmVivPwh0hR8sCTZItI6yG4fcY45Z/FiZXvbUh9Q8I5MRa5/ByEBWdbmX\r\nqBE+VyXYNKytxGlN0bSegD148v1CIIu+9dNJBLfGkzFbcvwkibGP/B+4Laia\r\nQtKlE2Im5DVaRmb+6FbWHK9KAiYVh9EXvJM/gZ9bclr/0HVzoTKHJZi0/jEs\r\nH0VWzzj3hfzzpM4sY24ngo3575WVzfM5MPNKVErImME1mrYxvvSqVpV1dL7Q\r\n9tNoJJwJFARcsHs3p7A7mGCWswk1b5wnt13gjEdWWgy6LeaNqh6CNTgzlPEg\r\nYj0B8d2NApel96zeD5kexZq0InVCbR/mDkFutFwfZTm3QK7NnUGqpDyKphIh\r\n10GPB2cpXUhtZyLUvy6Umj7fl7PuIrjheFZlGEuvylAERnYvuU6CndaDcOX1\r\naV8t/5nb0N1+im39vDK/vZ4mjeDZ7KySjtM=\r\n=xVEF\r\n-----END PGP SIGNATURE-----\r\n","size":6088,"noattachment":false,"key":"/tcp-base/-/tcp-base-3.1.1.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.1.1_1662044149868_0.28088717609108604"},"_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":1662044150049,"_cnpm_publish_time":1662044150049,"dist-tags":{"latest":"3.2.0"}}