Skip to content

Commit 65aa786

Browse files
committed
Added settings object to toggle each feature, updated documentation to show. Settings are now only changed from the server, the clients abide by those set settings
1 parent 4c3c8b9 commit 65aa786

File tree

4 files changed

+117
-37
lines changed

4 files changed

+117
-37
lines changed

Examples/ClientExample.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
let { TCPClient } = require('../Limitless-TCP');
22

3-
let tcpClient = new TCPClient('127.0.0.1', 1234, true);
3+
let tcpClient = new TCPClient('127.0.0.1', 1234);
44

55
tcpClient.connect();
66

Examples/ServerExample.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
let { TCPServer } = require('../Limitless-TCP')
1+
let { TCPServer } = require('../Limitless-TCP');
22

3-
let tcpServer = new TCPServer(1234, true);
3+
let settings = {
4+
useHeartbeat: true,
5+
useCompression: true
6+
}
7+
8+
let tcpServer = new TCPServer(1234, settings); //The settings here will be applied to any clients that connect to the server
49

510
tcpServer.listen();
611

Limitless-TCP.js

Lines changed: 95 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,33 @@
55
* Please refer to the JS docs for more information:
66
* https://github.com/Limitless-TCP/LimitlessTCP-JavaScript/blob/master/README.md
77
*/
8-
8+
99
const net = require('net');
10-
const pako = require('pako');
1110
const crypto = require('crypto');
1211

1312
class TCPClient {
14-
constructor(address, port, useHeartbeat) {
13+
14+
//TODO Add this settings object instead of useHearbeat
15+
constructor(address, port) {
1516

1617
this.isServerAnInstance = false;
18+
1719
this.useHeartbeat = false;
18-
if (useHeartbeat === null) {
19-
this.useHeartbeatSetting = false;
20-
}else {
21-
this.useHeartbeatSetting = useHeartbeat;
22-
}
20+
this.useCompression = false;
21+
this.useChunking = false;
2322

2423
this.port = port;
2524
this.address = address;
2625

2726
this.socket = new net.Socket;
28-
29-
30-
//this.socket.emit('error', new TCPServiceError('Heartbeat Timeout', 'ERRCONHBT'));
3127
}
3228

33-
connect() {
34-
this.socket.connect(this.port, this.address, () => {
29+
connect(callback) {
30+
this.socket.connect(this.port, this.address, cb => {
31+
if (callback !== null) {
32+
callback(cb);
33+
}
34+
3535
this.localAddress = this.socket.address().address;
3636
this.localPort = this.socket.address().port;
3737

@@ -49,12 +49,21 @@ class TCPClient {
4949
break;
5050

5151
case 'tcpsjs-connect':
52-
if (this.useHeartbeatSetting) {
52+
this.isServerAnInstance = true;
53+
54+
if (packet.data.useHeartbeat) {
5355
this.useHeartbeat = true;
5456
this.startHeartbeat();
5557
this.lastHeartbeat = Date.now();
58+
}
5659

57-
this.isServerAnInstance = true;
60+
if (packet.data.useCompression) {
61+
this.pako = require('pako');
62+
this.useCompression = true;
63+
}
64+
65+
if (packet.data.useChunking) {
66+
this.useChunking = true;
5867
}
5968
break;
6069
}
@@ -86,8 +95,14 @@ class TCPClient {
8695
try {
8796
data = JSON.stringify(data);
8897
}catch (e) {}
98+
8999
if (this.isServerAnInstance) {
90-
this.socket.write(JSON.stringify({ type: 'tcpsjs-packet', data: Buffer.from(pako.deflate(data)) }) + '<PacketSplitter>');
100+
if (this.useCompression) {
101+
this.socket.write(JSON.stringify({ type: 'tcpsjs-packet', data: Buffer.from(pako.deflate(data)) }) + '<PacketSplitter>');
102+
}else {
103+
this.socket.write(JSON.stringify({ type: 'tcpsjs-packet', data: data }) + '<PacketSplitter>');
104+
}
105+
91106
}else {
92107
this.socket.write(data);
93108
}
@@ -121,7 +136,13 @@ class TCPClient {
121136
packet = JSON.parse(packet);
122137
if (packet.type === 'tcpsjs-packet') {
123138

124-
let packetData = Buffer.from(pako.inflate(new Uint8Array(Buffer.from(packet.data.data)))).toString();
139+
let packetData;
140+
141+
if (this.useCompression) {
142+
packetData = Buffer.from(pako.inflate(new Uint8Array(Buffer.from(packet.data.data)))).toString();
143+
}else {
144+
packetData = packet.data.toString();
145+
}
125146

126147
try { //Try to parse, if error it isn't a json
127148
packetData = JSON.parse(packetData)
@@ -175,25 +196,58 @@ class TCPClient {
175196

176197

177198
class TCPServer {
178-
constructor(port, useHeartbeat) {
179-
this.port = port;
180-
181-
this.connectedSockets = [];
182-
this.allSockets = [];
183199

184-
this.server = net.createServer();
200+
/**
201+
* @param settings = { //If null, set to true (If server is instance of LimitlessTCP)
202+
* heartbeat: bool,
203+
* compression: bool, //Only import the compression library if this is true
204+
* chunking: bool //Coming soon
205+
* }
206+
*/
207+
constructor(port, settings) {
208+
this.port = port;
185209

186-
if (useHeartbeat === null) {
210+
if (settings === null) {
187211
this.useHeartbeat = false;
212+
this.useCompression = false;
213+
this.useChunking = false;
214+
}else {
215+
if (settings.useHeartbeat !== undefined) {
216+
this.useHeartbeat = settings.useHeartbeat;
217+
}else {
218+
this.useHeartbeat = true;
219+
}
188220

221+
if (settings.useCompression !== undefined) {
222+
this.useCompression = settings.useCompression;
189223

190-
}else {
191-
this.useHeartbeat = useHeartbeat;
224+
if (settings.useCompression) { //Load in compression library if enabled
225+
this.pako = require('pako');
226+
}
227+
}else {
228+
this.useCompression = true;
229+
this.pako = require('pako');
230+
}
231+
232+
if (settings.useChunking !== undefined) {
233+
this.useChunking = settings.useChunking;
234+
}else {
235+
this.useChunking = true;
236+
}
192237
}
238+
239+
this.connectedSockets = [];
240+
this.allSockets = [];
241+
242+
this.server = net.createServer();
193243
}
194244

195-
listen() {
196-
this.server.listen(this.port, () => {
245+
listen(callback) {
246+
this.server.listen(this.port, cb => {
247+
if (callback !== null) {
248+
callback(cb);
249+
}
250+
197251
if (this.useHeartbeat) {
198252
this.startHeartbeat();
199253
}
@@ -205,7 +259,7 @@ class TCPServer {
205259
this.connectedSockets.push(socket);
206260
this.allSockets.push(socket);
207261

208-
socket.write(JSON.stringify({ type: 'tcpsjs-connect' }))
262+
socket.write(JSON.stringify({ type: 'tcpsjs-connect', data: { useHeartbeat: this.useHeartbeat, useCompression: this.useCompression, useChunking: this.useChunking } }))
209263

210264
if (this.useHeartbeat) {
211265
socket.lastHeartbeat = Date.now();
@@ -259,7 +313,11 @@ class TCPServer {
259313
data = JSON.stringify(data);
260314
}catch (e) {}
261315

262-
socket.write(JSON.stringify({ type: 'tcpsjs-packet', data: Buffer.from(pako.deflate(data)) }) + '<PacketSplitter>');
316+
if (this.useCompression) {
317+
socket.write(JSON.stringify({ type: 'tcpsjs-packet', data: Buffer.from(pako.deflate(data)) }) + '<PacketSplitter>');
318+
}else {
319+
socket.write(JSON.stringify({ type: 'tcpsjs-packet', data: data }) + '<PacketSplitter>');
320+
}
263321
}
264322

265323
on(event, socket, callback) {
@@ -303,7 +361,13 @@ class TCPServer {
303361
packet = JSON.parse(packet);
304362
if (packet.type === 'tcpsjs-packet') {
305363

306-
let packetData = Buffer.from(pako.inflate(new Uint8Array(Buffer.from(packet.data.data)))).toString();
364+
let packetData;
365+
366+
if (this.useCompression) {
367+
packetData = Buffer.from(pako.inflate(new Uint8Array(Buffer.from(packet.data.data)))).toString();
368+
}else {
369+
packetData = packet.data.toString();
370+
}
307371

308372
try { //Try to parse, if error it isn't a json
309373
packetData = JSON.parse(packetData)

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Main features:
1111
* No limits from tcp
1212
* Built in heartbeats with timeout error
1313
* Built in packet compression using ZLib
14+
* Settings for each feature so you can setup the server YOUR way
1415

1516
A few things to watch out for:
1617
* Both the client and the server must have heartbeats set to true for it to work
@@ -21,11 +22,13 @@ Required Modules:
2122

2223
# Getting started
2324
## Client:
25+
The setup for a client is very straight forward, you create a new instance of the TCPClient class and pass the address and port
26+
of a tcp server into the constructor, you then run the connect() function to attempt to connect to the server.
2427

2528
```javascript
2629
let {TCPClient} = require('Limitless-TCP');
2730

28-
let tcpClient = new TCPClient( str: address, num: port, bool: useHeartbeat );
31+
let tcpClient = new TCPClient( str: address, num: port);
2932
tcpClient.connect();
3033

3134
tcpClient.on( str: event, (callback) => {} );
@@ -66,7 +69,7 @@ Note that the data returned is as a buffer because that is how tcp sends data, a
6669
```javascript
6770
let {TCPClient} = require('./Limitless-TCP');
6871

69-
let tcpClient = new TCPClient('127.0.0.1', 1234, true);
72+
let tcpClient = new TCPClient('127.0.0.1', 1234);
7073

7174
tcpClient.connect();
7275

@@ -80,11 +83,19 @@ tcpClient.on('connect', () => {
8083
This will not work the other way around, meaning you are not able to connect a normal tcp client to a Limitless TCP server.
8184

8285
## Server:
86+
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
87+
here.
8388

8489
```javascript
8590
let {TCPServer} = require('Limitless-TCP');
8691

87-
let tcpServer = new TCPServer( num: port, bool: useHeartbeat )
92+
/**
93+
* @param settings = { //Any null values will be set to true
94+
* useHeartbeat: bool,
95+
* useCompression: bool
96+
* }
97+
*/
98+
let tcpServer = new TCPServer( num: port, obj: settings )
8899

89100
tcpServer.listen();
90101

0 commit comments

Comments
 (0)