tcp-base
A base class for tcp client with basic functions
Last updated 3 years ago by gxcsoccer .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install tcp-base 
SYNC missed versions from official npm registry.

tcp-base

A base class for tcp client with basic functions

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Install

$ npm install tcp-base --save

Node.js >= 6.0.0 required

Usage

A quick guide to implement a tcp echo client

Client:

'use strict';

const TCPBase = require('tcp-base');

/**
 * A Simple Protocol:
 *   (4B): request id
 *   (4B): body length
 *   ------------------------------
 *   body data
 */
class Client extends TCPBase {
  getHeader() {
    return this.read(8);
  }

  getBodyLength(header) {
    return header.readInt32BE(4);
  }

  decode(body, header) {
    return {
      id: header.readInt32BE(0),
      data: body,
    };
  }

  // heartbeat packet
  get heartBeatPacket() {
    return new Buffer([ 255, 255, 255, 255, 0, 0, 0, 0 ]);
  }
}

const client = new Client({
  host: '127.0.0.1',
  port: 8080,
});

const body = new Buffer('hello');
const data = new Buffer(8 + body.length);
data.writeInt32BE(1, 0);
data.writeInt32BE(body.length, 4);
body.copy(data, 8, 0);

client.send({
  id: 1,
  data,
  timeout: 5000,
}, (err, res) => {
  if (err) {
    console.error(err);
  }
  console.log(res.toString()); // should echo 'hello'
});

Server:

'use strict';

const net = require('net');

const server = net.createServer(socket => {
  let header;
  let bodyLen;

  function readPacket() {
    if (bodyLen == null) {
      header = socket.read(8);
      if (!header) {
        return false;
      }
      bodyLen = header.readInt32BE(4);
    }

    if (bodyLen === 0) {
      socket.write(header);
    } else {
      const body = socket.read(bodyLen);
      if (!body) {
        return false;
      }
      socket.write(Buffer.concat([ header, body ]));
    }
    bodyLen = null;
    return true;
  }

  socket.on('readable', () => {
    try {
      let remaining = false;
      do {
        remaining = readPacket();
      }
      while (remaining);
    } catch (err) {
      console.error(err);
    }
  });
});
server.listen(8080);

MIT

Current Tags

  • 3.1.0                                ...           latest (3 years ago)

9 Versions

  • 3.1.0                                ...           3 years ago
  • 1.1.2                                ...           3 years ago
  • 1.1.1                                ...           3 years ago
  • 3.0.0                                ...           3 years ago
  • 2.0.0                                ...           4 years ago
  • 1.1.0                                ...           4 years ago
  • 1.0.2                                ...           4 years ago
  • 1.0.1                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
Downloads
Today 684
This Week 14,252
This Month 41,944
Last Day 1,048
Last Week 15,519
Last Month 58,938
Dependencies (2)
Dev Dependencies (10)

Copyright 2014 - 2016 © taobao.org |