redweb 0.5.5 → 0.5.6
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 +70 -112
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -37,7 +37,7 @@ const services = [
|
|
|
37
37
|
{
|
|
38
38
|
serviceName: '/submit-form',
|
|
39
39
|
method: METHODS.POST,
|
|
40
|
-
|
|
40
|
+
function: (req, res) => {
|
|
41
41
|
const { name, email, message } = req.body;
|
|
42
42
|
if (!name || !email || !message) {
|
|
43
43
|
return res.status(400).json({ error: 'All fields are required' });
|
|
@@ -74,56 +74,76 @@ const options = {
|
|
|
74
74
|
const app = new HttpsServer(options);
|
|
75
75
|
```
|
|
76
76
|
|
|
77
|
-
|
|
77
|
+
### WebSocket Server with Handlers
|
|
78
|
+
|
|
79
|
+
RedWeb now supports **handler-based routing** for WebSocket connections, allowing you to modularize and secure your WebSocket message handling logic.
|
|
80
|
+
|
|
81
|
+
#### Defining a Custom Handler
|
|
82
|
+
|
|
83
|
+
Handlers extend the `BaseHandler` class and manage their own connections and message types.
|
|
84
|
+
|
|
85
|
+
```javascript
|
|
86
|
+
const { BaseHandler } = require('redweb');
|
|
87
|
+
|
|
88
|
+
class ChatHandler extends BaseHandler {
|
|
89
|
+
constructor() {
|
|
90
|
+
super({
|
|
91
|
+
name: 'ChatHandler',
|
|
92
|
+
handlers: {
|
|
93
|
+
chat: (socket, data) => {
|
|
94
|
+
console.log(`Received chat message: ${data.message}`);
|
|
95
|
+
socket.send(JSON.stringify({ type: 'chatResponse', message: 'Hello!' }));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
module.exports = ChatHandler;
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
#### Setting Up a WebSocket Server with Handlers
|
|
78
106
|
|
|
79
107
|
```javascript
|
|
80
108
|
const { SocketServer } = require('redweb');
|
|
109
|
+
const ChatHandler = require('./ChatHandler');
|
|
81
110
|
|
|
82
111
|
const options = {
|
|
83
112
|
port: 3000,
|
|
113
|
+
handlerConfig: [ChatHandler],
|
|
84
114
|
connectionOpenCallback: (socket) => {
|
|
85
115
|
console.log('WebSocket client connected');
|
|
86
116
|
},
|
|
87
117
|
connectionCloseCallback: (socket) => {
|
|
88
118
|
console.log('WebSocket client disconnected');
|
|
89
|
-
},
|
|
90
|
-
messageHandlers: {
|
|
91
|
-
'msg': (socket, data) => {
|
|
92
|
-
console.log(data);
|
|
93
|
-
socket.send('guuuuuurl');
|
|
94
|
-
}
|
|
95
119
|
}
|
|
96
120
|
};
|
|
97
121
|
|
|
98
122
|
const socketServer = new SocketServer(options);
|
|
99
123
|
```
|
|
100
124
|
|
|
101
|
-
####
|
|
125
|
+
#### Client Communication with a Handler
|
|
126
|
+
|
|
127
|
+
The client must identify the handler during the initial connection with a `__handlerConnect` message.
|
|
102
128
|
|
|
103
129
|
```javascript
|
|
104
|
-
const
|
|
130
|
+
const WebSocket = require('ws');
|
|
105
131
|
|
|
106
|
-
const
|
|
107
|
-
port: 4443,
|
|
108
|
-
ssl: {
|
|
109
|
-
key: './path/to/key.pem',
|
|
110
|
-
cert: './path/to/cert.pem'
|
|
111
|
-
},
|
|
112
|
-
connectionOpenCallback: (socket) => {
|
|
113
|
-
console.log('WebSocket client connected');
|
|
114
|
-
},
|
|
115
|
-
connectionCloseCallback: (socket) => {
|
|
116
|
-
console.log('WebSocket client disconnected');
|
|
117
|
-
},
|
|
118
|
-
messageHandlers: {
|
|
119
|
-
'msg': (socket, data) => {
|
|
120
|
-
console.log(data);
|
|
121
|
-
socket.send('guuuuuurl');
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
};
|
|
132
|
+
const ws = new WebSocket('ws://localhost:3000');
|
|
125
133
|
|
|
126
|
-
|
|
134
|
+
ws.on('open', () => {
|
|
135
|
+
ws.send(JSON.stringify({
|
|
136
|
+
type: '__handlerConnect',
|
|
137
|
+
data: { handlerName: 'ChatHandler' }
|
|
138
|
+
}));
|
|
139
|
+
|
|
140
|
+
// Send a message to the handler
|
|
141
|
+
ws.send(JSON.stringify({ type: 'chat', message: 'Hi there!' }));
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
ws.on('message', (message) => {
|
|
145
|
+
console.log('Received:', message);
|
|
146
|
+
});
|
|
127
147
|
```
|
|
128
148
|
|
|
129
149
|
### Managing Connected Clients
|
|
@@ -140,12 +160,6 @@ const socketServer = new SocketServer({
|
|
|
140
160
|
},
|
|
141
161
|
connectionCloseCallback: (socket) => {
|
|
142
162
|
console.log('WebSocket client disconnected');
|
|
143
|
-
},
|
|
144
|
-
messageHandlers: {
|
|
145
|
-
'msg': (socket, data) => {
|
|
146
|
-
console.log(data);
|
|
147
|
-
socket.send('guuuuuurl');
|
|
148
|
-
}
|
|
149
163
|
}
|
|
150
164
|
});
|
|
151
165
|
|
|
@@ -153,6 +167,25 @@ const socketServer = new SocketServer({
|
|
|
153
167
|
console.log(socketServer.clients); // Map of clients by their IP addresses
|
|
154
168
|
```
|
|
155
169
|
|
|
170
|
+
### Backward-Compatible Message Handlers
|
|
171
|
+
|
|
172
|
+
If no handler is assigned during the initial connection, you can still use traditional `messageHandlers`.
|
|
173
|
+
|
|
174
|
+
```javascript
|
|
175
|
+
const { SocketServer } = require('redweb');
|
|
176
|
+
|
|
177
|
+
const options = {
|
|
178
|
+
port: 3000,
|
|
179
|
+
messageHandlers: {
|
|
180
|
+
echo: (socket, data) => {
|
|
181
|
+
socket.send(JSON.stringify({ type: 'echoResponse', message: data.message }));
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
const socketServer = new SocketServer(options);
|
|
187
|
+
```
|
|
188
|
+
|
|
156
189
|
## Options
|
|
157
190
|
|
|
158
191
|
### HttpServer and HttpsServer Options
|
|
@@ -172,84 +205,9 @@ console.log(socketServer.clients); // Map of clients by their IP addresses
|
|
|
172
205
|
- **connectionCloseCallback**: Function to execute once a client disconnects.
|
|
173
206
|
- **messageCallback**: Function to execute for every message received.
|
|
174
207
|
- **messageHandlers**: Object containing message handlers based on message type.
|
|
208
|
+
- **handlerConfig**: Array of handler classes to support handler-based routing.
|
|
175
209
|
- **ssl**: SSL configuration for SecureSocketServer (`{ key: './path/to/key.pem', cert: './path/to/cert.pem' }`).
|
|
176
210
|
|
|
177
|
-
## HowTo
|
|
178
|
-
|
|
179
|
-
### Setting Up an HTTP Server
|
|
180
|
-
|
|
181
|
-
1. **Install RedWeb**: Run `npm install redweb`.
|
|
182
|
-
2. **Create a new directory** (e.g., `public`) to store your static files and add an `index.html` file:
|
|
183
|
-
|
|
184
|
-
```html
|
|
185
|
-
<!DOCTYPE html>
|
|
186
|
-
<html lang="en">
|
|
187
|
-
<head>
|
|
188
|
-
<meta charset="UTF-8">
|
|
189
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
190
|
-
<title>RedWeb HTTP Server</title>
|
|
191
|
-
</head>
|
|
192
|
-
<body>
|
|
193
|
-
<h1>Welcome to RedWeb HTTP Server!</h1>
|
|
194
|
-
</body>
|
|
195
|
-
</html>
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
3. **Create a new file** (e.g., `server.js`) and add the following code:
|
|
199
|
-
|
|
200
|
-
```javascript
|
|
201
|
-
const { HttpServer } = require('redweb');
|
|
202
|
-
|
|
203
|
-
const services = [
|
|
204
|
-
{
|
|
205
|
-
serviceName: '/api/hello',
|
|
206
|
-
method: 'get',
|
|
207
|
-
function: (req, res) => {
|
|
208
|
-
res.send('Hello World!');
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
];
|
|
212
|
-
|
|
213
|
-
const options = {
|
|
214
|
-
port: 3000,
|
|
215
|
-
publicPaths: ['./public'],
|
|
216
|
-
services: services
|
|
217
|
-
};
|
|
218
|
-
|
|
219
|
-
const app = new HttpServer(options);
|
|
220
|
-
```
|
|
221
|
-
|
|
222
|
-
4. **Run your server**: Execute `node server.js`.
|
|
223
|
-
|
|
224
|
-
### Setting Up a WebSocket Server
|
|
225
|
-
|
|
226
|
-
1. **Install RedWeb**: Run `npm install redweb`.
|
|
227
|
-
2. **Create a new file** (e.g., `socketServer.js`) and add the following code:
|
|
228
|
-
|
|
229
|
-
```javascript
|
|
230
|
-
const { SocketServer } = require('redweb');
|
|
231
|
-
|
|
232
|
-
const options = {
|
|
233
|
-
port: 3000,
|
|
234
|
-
connectionOpenCallback: (socket) => {
|
|
235
|
-
console.log('WebSocket client connected');
|
|
236
|
-
},
|
|
237
|
-
connectionCloseCallback: (socket) => {
|
|
238
|
-
console.log('WebSocket client disconnected');
|
|
239
|
-
},
|
|
240
|
-
messageHandlers: {
|
|
241
|
-
'msg': (socket, data) => {
|
|
242
|
-
console.log(data);
|
|
243
|
-
socket.send('guuuuuurl');
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
};
|
|
247
|
-
|
|
248
|
-
const socketServer = new SocketServer(options);
|
|
249
|
-
```
|
|
250
|
-
|
|
251
|
-
3. **Run your server**: Execute `node socketServer.js`.
|
|
252
|
-
|
|
253
211
|
## License
|
|
254
212
|
|
|
255
213
|
MIT License
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "redweb",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.6",
|
|
4
4
|
"description": "A way to quickly set up an express server",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"cors": "^2.8.5",
|
|
18
18
|
"express": "^4.19.2",
|
|
19
|
+
"react-dom": "^18.3.1",
|
|
19
20
|
"ws": "^8.17.0"
|
|
20
21
|
},
|
|
21
22
|
"devDependencies": {
|