Skip to content

Commit

Permalink
Added autoupdating connectedSockets and allSockets array and updated …
Browse files Browse the repository at this point in the history
…readme to match. This is the last of the TODO's
  • Loading branch information
Bossslime committed Jul 12, 2023
1 parent b729695 commit 33a5b33
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ It provides an easy way to create tcp client and server connections with the abi
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
* Built in heartbeats with timeout error
Expand Down Expand Up @@ -88,7 +89,16 @@ tcpServer.on(str: event, null/socket: socket, (callback) => {}); //If null then
tcpServer.emit(data, socket: socket);
```

//Refer to tcp docs for callback information
### 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)
```javascript
let TCPServer; //Initialize and listen

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

Refer to tcp docs for callback information
### Events:
* Server:
* connect
Expand All @@ -100,4 +110,15 @@ tcpServer.emit(data, socket: socket);
* drain
* end
* error
* lookup
* lookup

# 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.
```bash
TCPServiceError [Heartbeat Error]: The heartbeat counter has timed out
at Timeout._onTimeout (Path\To\TCPService.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.'
}
```
32 changes: 19 additions & 13 deletions TCPService.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,10 @@
* - error
* - lookup
*/

//TODO Add a connectedSockets array to the server that will update with disconnects, and heartbeat timeouts
//TODO Send a packet from the server to the client if it should use packet splitters

let net = require('net');
const net = require('net');
const pako = require('pako');
const crypto = require('crypto');

class TCPClient {
constructor(address, port, useHeartbeat) {
Expand Down Expand Up @@ -162,9 +160,9 @@ class TCPClient {

case 'connection':
case 'connect':
this.socket.on('connect', (socket) => {
this.socket.on('connect', cb => {
setTimeout(() => { //Wait for the connection packet
callback(socket);
callback(cb);
}, 20);
});
break;
Expand Down Expand Up @@ -259,6 +257,8 @@ class TCPServer {
});

this.server.on('connection', (socket) => {
socket.id = crypto.randomUUID();

this.connectedSockets.push(socket);
this.allSockets.push(socket);

Expand Down Expand Up @@ -344,7 +344,10 @@ class TCPServer {
}else {
switch(event.toLowerCase()) {
case 'close':
socket.on('close', callback);
socket.on('close', cb => {
this.removeSocketFromConnectedSockets(socket);
callback(cb);
});
break;

case 'data':
Expand Down Expand Up @@ -412,18 +415,21 @@ class TCPServer {

if (socket.heartbeatCounter === 8) {
socket.emit('error', new TCPServiceError(ErrorType.HEARTBEAT, 'A client has timed out due to heartbeat', socket));

this.server.close(() => {
if (this.heartbeatInterval !== null && this.heartbeatInterval !== undefined) {
clearInterval(this.heartbeatInterval);
}
});
this.removeSocketFromConnectedSockets(socket);
}
}
}, 900)
}
}, 1000)
}

removeSocketFromConnectedSockets(socket) {
this.connectedSockets.forEach((loopSocket, index) => {
if (loopSocket.id === socket.id) {
this.connectedSockets.splice(index, 1);
}
});
}
}

module.exports = { TCPClient, TCPServer }
Expand Down

0 comments on commit 33a5b33

Please sign in to comment.