1.1.3 • Published 5 years ago

tcp-bridge v1.1.3

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

tcp-bridge

Description

This is a small framework for connecting 2 or more servers to each other by tcp.

The framework has the function of automatic reconnection to each server, the sending queue, the lifetime of the tasks for sending.

Installation

With yarn:

yarn add tcp-bridge

or with npm:

npm install tcp-bridge

Usage

For a simple connection of two servers on localhost, you can do this:

const BridgeClass = require('tcp-bridge');
const Bridge = new BridgeClass();
Bridge.addPoint({
    port: 2020
});
Bridge.addPoint({
    port: 3030
});

If possible, connections will be established to servers localhost:2020 and localhost:3030.

All data from server 1 will be sent to server 2. All data from server 2 will be sent to server 1.

Servers may be more

require('tcp-bridge') returns constructor of Bridge class. To create new instance of Bridge you can do this:

const BridgeClass = require('tcp-bridge');
const Bridge = new BridgeClass();

For debugging you can use debug npm package or with same functionality. Example debugging: Install debug package:

yarn add debug

or:

npm install debug

then code:

const BridgeClass = require('tcp-bridge');
const debug = require('debug');
const Bridge = new BridgeClass(debug);

and run script with debug env variable, ex.:

DEBUG=* node index.js

BridgeInstance functions

#addPoint(params) -> point uuid identifier

Adds a new server, initiates a connection and returns a unique identifier.

Signature:

BridgeInstance.addPoint({
    port,
    host,
    reconnectOnClose,
    reconnectOnHasSendData,
    checkTime,
    taskTimeout,
    enabledRedirect,
    encoding,
})
ParameterRequiredDescriptionDefault value
porttruePort of server
hostHostname (or ip) of server'127.0.0.1'
reconnectOnCloseauto reconnect on close connectiontrue
reconnectOnHasSendDataauto reconnect if connection is closed and clinet has data to sendfalse
checkTimetime to check new data for sending300 ms
taskTimeouttask lifetime (in ms), 0 for disable60000
enabledRedirectif false - data from this point will not be redirectedtrue
encodingSet the encoding for the socket as a Readable Stream. See readable.setEncoding() for more information.null

#removePoint(identifier)

Disconnects and removes server by identifier.

ParameterRequiredDescriptionDefault value
identifiertrueIdentifier of point (server)

#sendDataToPoint(identifier, data)

Send data to one point.

ParameterRequiredDescriptionDefault value
identifiertrueIdentifier of point (server)
datatrueBytes (as in net library in socket.send function)

#getPoint(identifier) -> point instance

Getting one point instance.

ParameterRequiredDescriptionDefault value
identifiertrueIdentifier of point (server)

Client (Point) functions and properties

#enableRedirect()

Data from this point will be redirected.

#disableRedirect()

Data from this point will not be redirected.

#on('data', callback) – event, triggered if data will be received

#on('ready') – event, triggered if connection is activated

property .connected – true, if connection to server is active

More examples

4 servers with debug and encoding UTF8

const debug = require('debug');
const BridgeClass = require('tcp-bridge');
const Bridge = new BridgeClass(debug);

const firstUid = Bridge.addPoint({
    port: 2020,
    encoding: 'utf8',
});
Bridge.addPoint({
    port: 3030,
    reconnectOnClose: false,
    reconnectOnHasSendData: true,
    encoding: 'utf8',
});
Bridge.addPoint({
    port: 4040,
    encoding: 'utf8',
});
Bridge.addPoint({
    port: 5050,
    encoding: 'utf8',
});

Bridge.removePoint(firstUid);

const lastUid = Bridge.addPoint({
    port: 6060,
    encoding: 'utf8',
});

const point = Bridge.getPoint(lastUid);
point.on('ready', data => {
    console.log('READY');
});
point.on('data', data => {
    console.log('DATA', data);
});
1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago