redweb 0.6.0 → 0.6.1
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 +26 -3
- package/package.json +1 -1
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;
|
|
@@ -109,6 +109,29 @@ declare module 'redweb' {
|
|
|
109
109
|
broadcast(message: object): void;
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
+
/**
|
|
113
|
+
* Base WebSocket server class.
|
|
114
|
+
*/
|
|
115
|
+
export class BaseSocketServer {
|
|
116
|
+
/**
|
|
117
|
+
* Map of connected WebSocket clients by their IP address.
|
|
118
|
+
*/
|
|
119
|
+
clients: Map<string, WebSocket>;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* List of handler instances currently registered with the server.
|
|
123
|
+
*/
|
|
124
|
+
handlers: BaseHandler[];
|
|
125
|
+
|
|
126
|
+
constructor(server: HTTPServer | HTTPSServer, options?: SocketServerOptions);
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Adds a new handler to the WebSocket server.
|
|
130
|
+
* @param HandlerClass - A class extending `BaseHandler` to add to the server.
|
|
131
|
+
*/
|
|
132
|
+
addHandler(HandlerClass: new () => BaseHandler): void;
|
|
133
|
+
}
|
|
134
|
+
|
|
112
135
|
/**
|
|
113
136
|
* HTTP server class.
|
|
114
137
|
*/
|
|
@@ -126,14 +149,14 @@ declare module 'redweb' {
|
|
|
126
149
|
/**
|
|
127
150
|
* WebSocket server class.
|
|
128
151
|
*/
|
|
129
|
-
export class SocketServer {
|
|
152
|
+
export class SocketServer extends BaseSocketServer {
|
|
130
153
|
constructor(options?: SocketServerOptions);
|
|
131
154
|
}
|
|
132
155
|
|
|
133
156
|
/**
|
|
134
157
|
* Secure WebSocket server class.
|
|
135
158
|
*/
|
|
136
|
-
export class SecureSocketServer {
|
|
159
|
+
export class SecureSocketServer extends BaseSocketServer {
|
|
137
160
|
constructor(options?: SocketServerOptions);
|
|
138
161
|
}
|
|
139
162
|
|