redweb 0.5.9 → 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 +20 -4
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
|
}
|
|
@@ -58,6 +58,20 @@ class BaseSocketServer {
|
|
|
58
58
|
return handlerConfig.map(HandlerClass => new HandlerClass());
|
|
59
59
|
}
|
|
60
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
|
+
|
|
61
75
|
/**
|
|
62
76
|
* Handles a new WebSocket connection.
|
|
63
77
|
* @param {WebSocket} socket - The WebSocket connection instance.
|
|
@@ -69,7 +83,9 @@ class BaseSocketServer {
|
|
|
69
83
|
if (this.clients.get(ip) !== undefined) {
|
|
70
84
|
const oldClient = this.clients.get(ip);
|
|
71
85
|
console.warn(`Client ${ip} already connected, disconnecting existing connection.`);
|
|
72
|
-
oldClient.send(
|
|
86
|
+
oldClient.send(
|
|
87
|
+
JSON.stringify({ msg: 'You are being disconnected because a new client is connected with your IP address.' })
|
|
88
|
+
);
|
|
73
89
|
oldClient.close();
|
|
74
90
|
}
|
|
75
91
|
this.clients.set(ip, socket);
|
|
@@ -77,9 +93,9 @@ class BaseSocketServer {
|
|
|
77
93
|
|
|
78
94
|
if (this.connectionOpenCallback) this.connectionOpenCallback(socket);
|
|
79
95
|
|
|
80
|
-
socket.on('message',
|
|
96
|
+
socket.on('message', message => this.initialMessageHandler(socket, message, ip));
|
|
81
97
|
socket.on('close', () => this.handleClose(socket, ip));
|
|
82
|
-
socket.on('error',
|
|
98
|
+
socket.on('error', error => this.handleError(socket, error, ip));
|
|
83
99
|
}
|
|
84
100
|
|
|
85
101
|
/**
|
|
@@ -132,7 +148,7 @@ class BaseSocketServer {
|
|
|
132
148
|
|
|
133
149
|
// Optional: Perform authentication with `key`
|
|
134
150
|
socket.removeAllListeners('message');
|
|
135
|
-
handler.newConnection(socket, data);
|
|
151
|
+
handler.newConnection(socket, connectMessage.data);
|
|
136
152
|
}
|
|
137
153
|
|
|
138
154
|
/**
|