redweb 0.5.8 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -2
- package/src/ws/BaseSocketServer.js +26 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "redweb",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "A way to quickly set up an express server",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@types/express": "^4.17.21",
|
|
23
23
|
"@types/jest": "^29.5.12",
|
|
24
|
-
"jest": "^29.7.0"
|
|
24
|
+
"jest": "^29.7.0",
|
|
25
|
+
"supertest": "^7.0.0"
|
|
25
26
|
}
|
|
26
27
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
const { randomUUID } = require('crypto');
|
|
2
1
|
const WebSocket = require('ws');
|
|
3
2
|
|
|
4
3
|
/**
|
|
@@ -59,6 +58,20 @@ class BaseSocketServer {
|
|
|
59
58
|
return handlerConfig.map(HandlerClass => new HandlerClass());
|
|
60
59
|
}
|
|
61
60
|
|
|
61
|
+
/**
|
|
62
|
+
* Adds a new handler to the WebSocket server.
|
|
63
|
+
* @param {new () => BaseHandler} HandlerClass - The handler class to add.
|
|
64
|
+
*/
|
|
65
|
+
addHandler(HandlerClass) {
|
|
66
|
+
const newHandler = new HandlerClass();
|
|
67
|
+
if (this.handlers.find(handler => handler.name === newHandler.name)) {
|
|
68
|
+
console.warn(`Handler with name '${newHandler.name}' already exists.`);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
this.handlers.push(newHandler);
|
|
72
|
+
console.log(`Handler '${newHandler.name}' added successfully.`);
|
|
73
|
+
}
|
|
74
|
+
|
|
62
75
|
/**
|
|
63
76
|
* Handles a new WebSocket connection.
|
|
64
77
|
* @param {WebSocket} socket - The WebSocket connection instance.
|
|
@@ -67,14 +80,22 @@ class BaseSocketServer {
|
|
|
67
80
|
handleConnection(socket, req) {
|
|
68
81
|
const ip = req.socket.remoteAddress;
|
|
69
82
|
console.log(`New client connected: ${ip}`);
|
|
70
|
-
this.clients.
|
|
83
|
+
if (this.clients.get(ip) !== undefined) {
|
|
84
|
+
const oldClient = this.clients.get(ip);
|
|
85
|
+
console.warn(`Client ${ip} already connected, disconnecting existing connection.`);
|
|
86
|
+
oldClient.send(
|
|
87
|
+
JSON.stringify({ msg: 'You are being disconnected because a new client is connected with your IP address.' })
|
|
88
|
+
);
|
|
89
|
+
oldClient.close();
|
|
90
|
+
}
|
|
91
|
+
this.clients.set(ip, socket);
|
|
71
92
|
socket.isAssigned = false; // Tracks whether the socket has been assigned a handler.
|
|
72
93
|
|
|
73
94
|
if (this.connectionOpenCallback) this.connectionOpenCallback(socket);
|
|
74
95
|
|
|
75
|
-
socket.on('message',
|
|
96
|
+
socket.on('message', message => this.initialMessageHandler(socket, message, ip));
|
|
76
97
|
socket.on('close', () => this.handleClose(socket, ip));
|
|
77
|
-
socket.on('error',
|
|
98
|
+
socket.on('error', error => this.handleError(socket, error, ip));
|
|
78
99
|
}
|
|
79
100
|
|
|
80
101
|
/**
|
|
@@ -127,7 +148,7 @@ class BaseSocketServer {
|
|
|
127
148
|
|
|
128
149
|
// Optional: Perform authentication with `key`
|
|
129
150
|
socket.removeAllListeners('message');
|
|
130
|
-
handler.newConnection(socket, data);
|
|
151
|
+
handler.newConnection(socket, connectMessage.data);
|
|
131
152
|
}
|
|
132
153
|
|
|
133
154
|
/**
|