1.0.7 • Published 9 months ago

limitless_tcp v1.0.7

Weekly downloads
-
License
GPL-3.0
Repository
github
Last release
9 months ago

LimitlessTCP-JavaScript

This service is a wrapper for the TCP "Net" module. It provides an easy way to create tcp client and server connections with the ability to create networks with ease.

Main features:

  • Easy to setup
  • Anti-Packet stacking
  • Built in self auto updating connected and all sockets array
  • No need to stringify or parse JSON's, the data you send is the data you receive, no annoying buffers
  • No limits from tcp
  • The only speed limit is your network, in closed networks, it is able to perfectly handle packets through a for loop
  • Built in heartbeats with timeout error
  • Built in packet compression using ZLib
  • Settings for each feature so you can setup the server YOUR way

Beta Features:

  • Built in packet chunking system (This feature is inconsistent, it depends on the computer that it is running on)

A few things to watch out for:

  • Both the client and the server must have heartbeats set to true for it to work

Required Modules:

  • Pako (ZLib compression library)

The Best Part

The best part about this library, is the syntax is the exact same as regular tcp, meaning that you can fully migrate your project over without any hassle

Getting started

Client:

The setup for a client is very straight forward, you create a new instance of the TCPClient class and pass the address and port of a tcp server into the constructor, you then run the connect() function to attempt to connect to the server.

let { TCPClient } = require('Limitless-TCP');

let tcpClient = new TCPClient( str: address, num: port);
tcpClient.connect();

tcpClient.on( str: event, ( callback ) => {} );
tcpClient.emit(data);

Events:

  • close
  • connect
  • data
  • drain
  • end
  • error
  • lookup

Refer to tcp docs for callback information

Compatible with normal TCP

A Limitless-TCP Client is compatible with a normal TCP server, meaning that you are able to connect to a generic tcp server while using Limitless-TCP's simple syntax. This however does remove the 'heartbeat', 'compression', and 'anti packet stacking'.

Server:

let net = require('net');

let server = net.createServer();

server.listen(1234, () => {});

server.on('connection', ( socket ) => {
    socket.on('data', ( data ) => {
        console.log(data.toString());
    });
});

Note that the data returned is as a buffer because that is how tcp sends data, and the packets sent by the client are sent as stacked because they were sent too fast.

Client:

let { TCPClient } = require('Limitless-TCP');

let tcpClient = new TCPClient('127.0.0.1', 1234);

tcpClient.connect();

tcpClient.on('connect', () => {
    tcpClient.emit({type: 'test', data: 'This is a test packet 1'});
    tcpClient.emit({type: 'test', data: 'This is a test packet 2'});
    tcpClient.emit('Yo 1');
    tcpClient.emit('Yo 2');
});

This will not work the other way around, meaning you are not able to connect a normal tcp client to a Limitless TCP server.

Server:

The server is where all the settings are setup, meaning if you want to disable a feature, this is where you set it up, the clients will abide by the settings specified here.

let { TCPServer } = require('Limitless-TCP');

/**
 * @param settings = { //Any null values will be set to true
 *     useHeartbeat: bool,
 *     useCompression: bool,
 *     useChunking: bool (Beta Feature)
 * }
 */
let tcpServer = new TCPServer( num: port, obj: settings )

tcpServer.listen();

tcpServer.on( str: event, ( callback ) => {} );

Writing a message to socket

tcpServer.on( 'connect', ( socket ) => {
    socket.emit( any: message );
    socket.write( any: message );
});

This is also how you would achieve chunking, it is built in and automatically detected when a packet gets overs 14000 bytes. Setting useChunking to false would just attempt to send the packet in one go no matter the size.

Connected Sockets and All Sockets:

There is a built-in, auto updating array with all the connected sockets and every socket that is and has been connected (In its runtime, a restart would reset this)

let TCPServer; //Initialize and listen

let arr: connectedSockets  = TCPServer.connectedSockets;
let arr: allSockets        = TCPServer.allSockets;

Events:

  • Server:
    • connect
    • error
    • close
  • Client:
    • close
    • data
    • drain
    • end
    • error
    • lookup

Refer to tcp docs for callback information

Heartbeat Timeout

There is a different error that is thrown when the heartbeats timeout. This error is the same on the server and the client.

TCPServiceError [Heartbeat Error]: The heartbeat counter has timed out
    at Timeout._onTimeout (Path\To\Limitless-TCP.js:225:43)
    at listOnTimeout (node:internal/timers:564:17)
    at process.processTimers (node:internal/timers:507:7) {
  Details: 'This socket has timed out from the server.'
}
1.0.7

9 months ago

1.0.6

9 months ago

1.0.5

9 months ago

1.0.4

9 months ago

1.0.3

9 months ago

1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago