redweb 0.6.2 → 0.6.5
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/LICENSE +20 -20
- package/README.md +63 -85
- package/index.d.ts +255 -199
- package/index.js +13 -12
- package/package.json +27 -27
- package/src/http/BaseHttpServer.js +69 -69
- package/src/http/HttpServer.js +14 -14
- package/src/http/HttpsServer.js +17 -17
- package/src/http/index.js +1 -1
- package/src/sslConfig.js +20 -20
- package/src/ws/BaseHandler.js +39 -93
- package/src/ws/BaseSocketServer.js +57 -203
- package/src/ws/DefaultHandler.js +14 -0
- package/src/ws/DefaultRoute.js +14 -0
- package/src/ws/SecureSocketServer.js +20 -20
- package/src/ws/SocketRoute.js +114 -0
- package/src/ws/SocketServer.js +18 -18
- package/src/ws/index.js +6 -5
- package/src/ws/util.js +9 -0
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2024 Arkam Mazrui
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Arkam Mazrui
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -74,9 +74,9 @@ const options = {
|
|
|
74
74
|
const app = new HttpsServer(options);
|
|
75
75
|
```
|
|
76
76
|
|
|
77
|
-
### WebSocket Server with Handlers
|
|
77
|
+
### WebSocket Server with Routes and Handlers
|
|
78
78
|
|
|
79
|
-
RedWeb
|
|
79
|
+
RedWeb uses **route-based architecture** for WebSocket connections, allowing you to modularize and secure your WebSocket message handling logic.
|
|
80
80
|
|
|
81
81
|
#### Defining a Custom Handler
|
|
82
82
|
|
|
@@ -87,86 +87,85 @@ const { BaseHandler } = require('redweb');
|
|
|
87
87
|
|
|
88
88
|
class ChatHandler extends BaseHandler {
|
|
89
89
|
constructor() {
|
|
90
|
-
super(
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
});
|
|
90
|
+
super('chat');
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
onMessage(socket, message) {
|
|
94
|
+
console.log(`Received chat message: ${message.text}`);
|
|
95
|
+
socket.send(JSON.stringify({ type: 'chatResponse', message: 'Hello!' }));
|
|
99
96
|
}
|
|
100
97
|
}
|
|
101
98
|
|
|
102
99
|
module.exports = ChatHandler;
|
|
103
100
|
```
|
|
104
101
|
|
|
105
|
-
####
|
|
102
|
+
#### Defining a WebSocket Route
|
|
103
|
+
|
|
104
|
+
Routes group handlers and specify the WebSocket path.
|
|
106
105
|
|
|
107
106
|
```javascript
|
|
108
|
-
const {
|
|
107
|
+
const { SocketRoute } = require('redweb');
|
|
109
108
|
const ChatHandler = require('./ChatHandler');
|
|
110
109
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
connectionCloseCallback: (socket) => {
|
|
118
|
-
console.log('WebSocket client disconnected');
|
|
110
|
+
class ChatRoute extends SocketRoute {
|
|
111
|
+
constructor() {
|
|
112
|
+
super({
|
|
113
|
+
path: '/chat',
|
|
114
|
+
handlers: [ChatHandler]
|
|
115
|
+
});
|
|
119
116
|
}
|
|
120
|
-
}
|
|
117
|
+
}
|
|
121
118
|
|
|
122
|
-
|
|
119
|
+
module.exports = ChatRoute;
|
|
123
120
|
```
|
|
124
121
|
|
|
125
|
-
####
|
|
122
|
+
#### Setting Up a WebSocket Server with Routes
|
|
126
123
|
|
|
127
|
-
|
|
124
|
+
```javascript
|
|
125
|
+
const { SocketServer } = require('redweb');
|
|
126
|
+
const ChatRoute = require('./ChatRoute');
|
|
127
|
+
|
|
128
|
+
new SocketServer({
|
|
129
|
+
port: 3000,
|
|
130
|
+
routes: [ChatRoute]
|
|
131
|
+
});
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Adding Routes Dynamically
|
|
135
|
+
|
|
136
|
+
Routes can be added to the WebSocket server after initialization.
|
|
128
137
|
|
|
129
138
|
```javascript
|
|
130
|
-
|
|
131
|
-
|
|
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
|
-
}
|
|
139
|
+
const { SocketServer, SocketRoute } = require('redweb');
|
|
140
|
+
const ChatHandler = require('./ChatHandler');
|
|
140
141
|
|
|
141
|
-
|
|
142
|
-
|
|
142
|
+
class ChatRoute extends SocketRoute {
|
|
143
|
+
constructor() {
|
|
144
|
+
super({
|
|
145
|
+
path: '/chat',
|
|
146
|
+
handlers: [ChatHandler]
|
|
147
|
+
});
|
|
143
148
|
}
|
|
144
149
|
}
|
|
145
150
|
|
|
146
|
-
const { SocketServer } = require('redweb');
|
|
147
151
|
const socketServer = new SocketServer({ port: 3000 });
|
|
148
152
|
|
|
149
|
-
// Dynamically add a new
|
|
150
|
-
|
|
153
|
+
// Dynamically add a new route
|
|
154
|
+
const chatRoute = new ChatRoute();
|
|
155
|
+
socketServer.routes.push(chatRoute);
|
|
151
156
|
```
|
|
152
157
|
|
|
153
|
-
|
|
158
|
+
### Client Communication with a Route
|
|
154
159
|
|
|
155
|
-
The client
|
|
160
|
+
The client connects to the WebSocket server using the specified route.
|
|
156
161
|
|
|
157
162
|
```javascript
|
|
158
163
|
const WebSocket = require('ws');
|
|
159
164
|
|
|
160
|
-
const ws = new WebSocket('ws://localhost:3000');
|
|
165
|
+
const ws = new WebSocket('ws://localhost:3000/chat');
|
|
161
166
|
|
|
162
167
|
ws.on('open', () => {
|
|
163
|
-
ws.send(JSON.stringify({
|
|
164
|
-
type: '__handlerConnect',
|
|
165
|
-
data: { handlerName: 'ChatHandler' }
|
|
166
|
-
}));
|
|
167
|
-
|
|
168
|
-
// Send a message to the handler
|
|
169
|
-
ws.send(JSON.stringify({ type: 'chat', message: 'Hi there!' }));
|
|
168
|
+
ws.send(JSON.stringify({ type: 'chat', text: 'Hello there!' }));
|
|
170
169
|
});
|
|
171
170
|
|
|
172
171
|
ws.on('message', (message) => {
|
|
@@ -176,42 +175,25 @@ ws.on('message', (message) => {
|
|
|
176
175
|
|
|
177
176
|
### Managing Connected Clients
|
|
178
177
|
|
|
179
|
-
RedWeb's WebSocket server maintains a list of connected clients by their IP addresses. This list is automatically updated when clients connect or disconnect.
|
|
178
|
+
RedWeb's WebSocket server maintains a list of connected clients by their IP addresses for each route. This list is automatically updated when clients connect or disconnect.
|
|
180
179
|
|
|
181
180
|
```javascript
|
|
182
|
-
const {
|
|
181
|
+
const { SocketRoute } = require('redweb');
|
|
183
182
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
console.log('WebSocket client disconnected');
|
|
183
|
+
class ChatRoute extends SocketRoute {
|
|
184
|
+
constructor() {
|
|
185
|
+
super({
|
|
186
|
+
path: '/chat',
|
|
187
|
+
handlers: []
|
|
188
|
+
});
|
|
191
189
|
}
|
|
192
|
-
});
|
|
193
190
|
|
|
194
|
-
|
|
195
|
-
console.log(
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
### Backward-Compatible Message Handlers
|
|
199
|
-
|
|
200
|
-
If no handler is assigned during the initial connection, you can still use traditional `messageHandlers`.
|
|
201
|
-
|
|
202
|
-
```javascript
|
|
203
|
-
const { SocketServer } = require('redweb');
|
|
204
|
-
|
|
205
|
-
const options = {
|
|
206
|
-
port: 3000,
|
|
207
|
-
messageHandlers: {
|
|
208
|
-
echo: (socket, data) => {
|
|
209
|
-
socket.send(JSON.stringify({ type: 'echoResponse', message: data.message }));
|
|
210
|
-
}
|
|
191
|
+
onConnection(socket) {
|
|
192
|
+
console.log('New client connected:', socket.remoteAddress);
|
|
211
193
|
}
|
|
212
|
-
}
|
|
194
|
+
}
|
|
213
195
|
|
|
214
|
-
|
|
196
|
+
module.exports = ChatRoute;
|
|
215
197
|
```
|
|
216
198
|
|
|
217
199
|
## Options
|
|
@@ -226,14 +208,10 @@ const socketServer = new SocketServer(options);
|
|
|
226
208
|
- **encoding**: Encoding type for request bodies (`'json'` or `'urlencoded'`).
|
|
227
209
|
- **ssl**: SSL configuration for HTTPS server (`{ key: './path/to/key.pem', cert: './path/to/cert.pem' }`).
|
|
228
210
|
|
|
229
|
-
### SocketServer
|
|
211
|
+
### SocketServer Options
|
|
230
212
|
|
|
231
213
|
- **port**: Port number (default: `3000`).
|
|
232
|
-
- **
|
|
233
|
-
- **connectionCloseCallback**: Function to execute once a client disconnects.
|
|
234
|
-
- **messageCallback**: Function to execute for every message received.
|
|
235
|
-
- **messageHandlers**: Object containing message handlers based on message type.
|
|
236
|
-
- **handlerConfig**: Array of handler classes to support handler-based routing.
|
|
214
|
+
- **routes**: Array of `SocketRoute` classes to define WebSocket routes and handlers.
|
|
237
215
|
- **ssl**: SSL configuration for SecureSocketServer (`{ key: './path/to/key.pem', cert: './path/to/cert.pem' }`).
|
|
238
216
|
|
|
239
217
|
## License
|