redweb 0.6.0 → 0.6.2
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/README.md +29 -1
- package/index.d.ts +29 -5
- package/package.json +1 -1
- package/src/ws/BaseHandler.js +17 -7
package/README.md
CHANGED
|
@@ -76,7 +76,7 @@ const app = new HttpsServer(options);
|
|
|
76
76
|
|
|
77
77
|
### WebSocket Server with Handlers
|
|
78
78
|
|
|
79
|
-
RedWeb
|
|
79
|
+
RedWeb supports **handler-based routing** for WebSocket connections, allowing you to modularize and secure your WebSocket message handling logic.
|
|
80
80
|
|
|
81
81
|
#### Defining a Custom Handler
|
|
82
82
|
|
|
@@ -122,6 +122,34 @@ const options = {
|
|
|
122
122
|
const socketServer = new SocketServer(options);
|
|
123
123
|
```
|
|
124
124
|
|
|
125
|
+
#### Adding Handlers Dynamically
|
|
126
|
+
|
|
127
|
+
Handlers can be added after the WebSocket server has started using the `addHandler` method.
|
|
128
|
+
|
|
129
|
+
```javascript
|
|
130
|
+
class DynamicHandler {
|
|
131
|
+
constructor() {
|
|
132
|
+
this.name = 'DynamicHandler';
|
|
133
|
+
this.handlers = {
|
|
134
|
+
dynamicMessage: (socket, data) => {
|
|
135
|
+
console.log(`Received dynamic message: ${data.message}`);
|
|
136
|
+
socket.send(JSON.stringify({ type: 'dynamicResponse', message: 'Handled dynamically!' }));
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
newConnection(socket) {
|
|
142
|
+
console.log('DynamicHandler: New connection established');
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const { SocketServer } = require('redweb');
|
|
147
|
+
const socketServer = new SocketServer({ port: 3000 });
|
|
148
|
+
|
|
149
|
+
// Dynamically add a new handler
|
|
150
|
+
socketServer.addHandler(DynamicHandler);
|
|
151
|
+
```
|
|
152
|
+
|
|
125
153
|
#### Client Communication with a Handler
|
|
126
154
|
|
|
127
155
|
The client must identify the handler during the initial connection with a `__handlerConnect` message.
|
package/index.d.ts
CHANGED
|
@@ -35,7 +35,7 @@ declare module 'redweb' {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
|
-
*
|
|
38
|
+
* Configuration for a WebSocket handler.
|
|
39
39
|
*/
|
|
40
40
|
export interface HandlerConfig {
|
|
41
41
|
name: string;
|
|
@@ -90,10 +90,11 @@ declare module 'redweb' {
|
|
|
90
90
|
constructor(config: HandlerConfig);
|
|
91
91
|
|
|
92
92
|
/**
|
|
93
|
-
* Adds a new WebSocket connection and sets up message handling for this handler.
|
|
93
|
+
* Adds a new WebSocket connection, processes initial data, and sets up message handling for this handler.
|
|
94
94
|
* @param socket - The WebSocket connection to add.
|
|
95
|
+
* @param data - Optional initial data sent during the connection handshake.
|
|
95
96
|
*/
|
|
96
|
-
newConnection(socket: WebSocket): void;
|
|
97
|
+
newConnection(socket: WebSocket, data?: any): void;
|
|
97
98
|
|
|
98
99
|
/**
|
|
99
100
|
* Handles an incoming message and routes it to the appropriate handler function.
|
|
@@ -109,6 +110,29 @@ declare module 'redweb' {
|
|
|
109
110
|
broadcast(message: object): void;
|
|
110
111
|
}
|
|
111
112
|
|
|
113
|
+
/**
|
|
114
|
+
* Base WebSocket server class.
|
|
115
|
+
*/
|
|
116
|
+
export class BaseSocketServer {
|
|
117
|
+
/**
|
|
118
|
+
* Map of connected WebSocket clients by their IP address.
|
|
119
|
+
*/
|
|
120
|
+
clients: Map<string, WebSocket>;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* List of handler instances currently registered with the server.
|
|
124
|
+
*/
|
|
125
|
+
handlers: BaseHandler[];
|
|
126
|
+
|
|
127
|
+
constructor(server: HTTPServer | HTTPSServer, options?: SocketServerOptions);
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Adds a new handler to the WebSocket server.
|
|
131
|
+
* @param HandlerClass - A class extending `BaseHandler` to add to the server.
|
|
132
|
+
*/
|
|
133
|
+
addHandler(HandlerClass: new () => BaseHandler): void;
|
|
134
|
+
}
|
|
135
|
+
|
|
112
136
|
/**
|
|
113
137
|
* HTTP server class.
|
|
114
138
|
*/
|
|
@@ -126,14 +150,14 @@ declare module 'redweb' {
|
|
|
126
150
|
/**
|
|
127
151
|
* WebSocket server class.
|
|
128
152
|
*/
|
|
129
|
-
export class SocketServer {
|
|
153
|
+
export class SocketServer extends BaseSocketServer {
|
|
130
154
|
constructor(options?: SocketServerOptions);
|
|
131
155
|
}
|
|
132
156
|
|
|
133
157
|
/**
|
|
134
158
|
* Secure WebSocket server class.
|
|
135
159
|
*/
|
|
136
|
-
export class SecureSocketServer {
|
|
160
|
+
export class SecureSocketServer extends BaseSocketServer {
|
|
137
161
|
constructor(options?: SocketServerOptions);
|
|
138
162
|
}
|
|
139
163
|
|
package/package.json
CHANGED
package/src/ws/BaseHandler.js
CHANGED
|
@@ -28,24 +28,34 @@ class BaseHandler {
|
|
|
28
28
|
this.connections = [];
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
* Adds a new WebSocket connection and sets up message handling for this handler.
|
|
33
|
-
* @param {WebSocket} socket - The WebSocket connection to add.
|
|
34
|
-
*/
|
|
35
|
-
newConnection(socket) {
|
|
36
|
-
this.connections.push(socket);
|
|
37
|
-
|
|
31
|
+
handleSocketMessages(socket) {
|
|
38
32
|
socket.on('message', (message) => this.handleMessage(socket, message));
|
|
33
|
+
}
|
|
39
34
|
|
|
35
|
+
handleSocketClose(socket) {
|
|
40
36
|
socket.on('close', () => {
|
|
41
37
|
this.connections = this.connections.filter((conn) => conn !== socket);
|
|
42
38
|
console.log(`Connection closed for handler "${this.name}".`);
|
|
43
39
|
});
|
|
40
|
+
}
|
|
44
41
|
|
|
42
|
+
handleNewConnection(socket) {
|
|
43
|
+
this.connections.push(socket);
|
|
44
|
+
this.handleSocketMessages(socket);
|
|
45
|
+
this.handleSocketClose(socket);
|
|
45
46
|
socket.isAssigned = true;
|
|
46
47
|
console.log(`New connection added to handler "${this.name}".`);
|
|
47
48
|
}
|
|
48
49
|
|
|
50
|
+
/**
|
|
51
|
+
* Adds a new WebSocket connection and sets up message handling for this handler.
|
|
52
|
+
* @param {WebSocket} socket - The WebSocket connection to add.
|
|
53
|
+
*/
|
|
54
|
+
newConnection(socket, data) {
|
|
55
|
+
console.log(`Received newConnection data ${data}`);
|
|
56
|
+
this.handleNewConnection(socket);
|
|
57
|
+
}
|
|
58
|
+
|
|
49
59
|
/**
|
|
50
60
|
* Handles an incoming message and routes it to the appropriate handler function.
|
|
51
61
|
* @param {WebSocket} socket - The WebSocket connection that sent the message.
|