Skip to content

Commit 33a5b33

Browse files
committed
Added autoupdating connectedSockets and allSockets array and updated readme to match. This is the last of the TODO's
1 parent b729695 commit 33a5b33

2 files changed

Lines changed: 42 additions & 15 deletions

File tree

README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ It provides an easy way to create tcp client and server connections with the abi
66
Main features:
77
* Easy to setup
88
* Anti-Packet stacking
9+
* Built in self auto updating connected and all sockets array
910
* No need to stringify or parse JSON's, the data you send is the data you receive, no annoying buffers
1011
* No limits from tcp
1112
* Built in heartbeats with timeout error
@@ -88,7 +89,16 @@ tcpServer.on(str: event, null/socket: socket, (callback) => {}); //If null then
8889
tcpServer.emit(data, socket: socket);
8990
```
9091

91-
//Refer to tcp docs for callback information
92+
### Connected Sockets and All Sockets:
93+
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)
94+
```javascript
95+
let TCPServer; //Initialize and listen
96+
97+
let arr: connectedSockets = TCPServer.connectedSockets;
98+
let arr: allSockets = TCPServer.allSockets;
99+
```
100+
101+
Refer to tcp docs for callback information
92102
### Events:
93103
* Server:
94104
* connect
@@ -100,4 +110,15 @@ tcpServer.emit(data, socket: socket);
100110
* drain
101111
* end
102112
* error
103-
* lookup
113+
* lookup
114+
115+
# Heartbeat Timeout
116+
There is a different error that is thrown when the heartbeats timeout. This error is the same on the server and the client.
117+
```bash
118+
TCPServiceError [Heartbeat Error]: The heartbeat counter has timed out
119+
at Timeout._onTimeout (Path\To\TCPService.js:225:43)
120+
at listOnTimeout (node:internal/timers:564:17)
121+
at process.processTimers (node:internal/timers:507:7) {
122+
Details: 'This socket has timed out from the server.'
123+
}
124+
```

TCPService.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,10 @@
6262
* - error
6363
* - lookup
6464
*/
65-
66-
//TODO Add a connectedSockets array to the server that will update with disconnects, and heartbeat timeouts
67-
//TODO Send a packet from the server to the client if it should use packet splitters
6865

69-
let net = require('net');
66+
const net = require('net');
7067
const pako = require('pako');
68+
const crypto = require('crypto');
7169

7270
class TCPClient {
7371
constructor(address, port, useHeartbeat) {
@@ -162,9 +160,9 @@ class TCPClient {
162160

163161
case 'connection':
164162
case 'connect':
165-
this.socket.on('connect', (socket) => {
163+
this.socket.on('connect', cb => {
166164
setTimeout(() => { //Wait for the connection packet
167-
callback(socket);
165+
callback(cb);
168166
}, 20);
169167
});
170168
break;
@@ -259,6 +257,8 @@ class TCPServer {
259257
});
260258

261259
this.server.on('connection', (socket) => {
260+
socket.id = crypto.randomUUID();
261+
262262
this.connectedSockets.push(socket);
263263
this.allSockets.push(socket);
264264

@@ -344,7 +344,10 @@ class TCPServer {
344344
}else {
345345
switch(event.toLowerCase()) {
346346
case 'close':
347-
socket.on('close', callback);
347+
socket.on('close', cb => {
348+
this.removeSocketFromConnectedSockets(socket);
349+
callback(cb);
350+
});
348351
break;
349352

350353
case 'data':
@@ -412,18 +415,21 @@ class TCPServer {
412415

413416
if (socket.heartbeatCounter === 8) {
414417
socket.emit('error', new TCPServiceError(ErrorType.HEARTBEAT, 'A client has timed out due to heartbeat', socket));
415-
416-
this.server.close(() => {
417-
if (this.heartbeatInterval !== null && this.heartbeatInterval !== undefined) {
418-
clearInterval(this.heartbeatInterval);
419-
}
420-
});
418+
this.removeSocketFromConnectedSockets(socket);
421419
}
422420
}
423421
}, 900)
424422
}
425423
}, 1000)
426424
}
425+
426+
removeSocketFromConnectedSockets(socket) {
427+
this.connectedSockets.forEach((loopSocket, index) => {
428+
if (loopSocket.id === socket.id) {
429+
this.connectedSockets.splice(index, 1);
430+
}
431+
});
432+
}
427433
}
428434

429435
module.exports = { TCPClient, TCPServer }

0 commit comments

Comments
 (0)