From 33a5b33791e66ad1b6f1a13c79e4b5b860e8765b Mon Sep 17 00:00:00 2001 From: Bossslime Date: Wed, 12 Jul 2023 12:33:01 -0400 Subject: [PATCH] Added autoupdating connectedSockets and allSockets array and updated readme to match. This is the last of the TODO's --- README.md | 25 +++++++++++++++++++++++-- TCPService.js | 32 +++++++++++++++++++------------- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 1574835..7f7fa25 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -100,4 +110,15 @@ tcpServer.emit(data, socket: socket); * drain * end * error - * lookup \ No newline at end of file + * 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.' +} +``` \ No newline at end of file diff --git a/TCPService.js b/TCPService.js index b33bbe3..ecf903d 100644 --- a/TCPService.js +++ b/TCPService.js @@ -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) { @@ -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; @@ -259,6 +257,8 @@ class TCPServer { }); this.server.on('connection', (socket) => { + socket.id = crypto.randomUUID(); + this.connectedSockets.push(socket); this.allSockets.push(socket); @@ -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': @@ -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 }