redweb 0.2.5 → 0.2.7
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 +74 -71
- package/SocketServer.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# RedWeb
|
|
2
2
|
|
|
3
|
-
RedWeb is a simple and flexible Node.js framework built on top of Express.js. It allows you to quickly set up
|
|
3
|
+
RedWeb is a simple and flexible Node.js framework built on top of Express.js and WebSocket. It allows you to quickly set up web servers and WebSocket servers with customizable options.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -14,17 +14,21 @@ npm install redweb
|
|
|
14
14
|
|
|
15
15
|
### Basic Example
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
Initialize your RedWeb instance with the default options:
|
|
18
18
|
|
|
19
19
|
```javascript
|
|
20
|
-
const { HttpServer } = require('redweb');
|
|
20
|
+
const { HttpServer, SocketServer } = require('redweb');
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
// HTTP server with default configuration
|
|
23
|
+
const httpServer = new HttpServer();
|
|
24
|
+
|
|
25
|
+
// WebSocket server with default configuration
|
|
26
|
+
const socketServer = new SocketServer();
|
|
23
27
|
```
|
|
24
28
|
|
|
25
29
|
### Custom Configuration
|
|
26
30
|
|
|
27
|
-
|
|
31
|
+
#### HTTP Server
|
|
28
32
|
|
|
29
33
|
```javascript
|
|
30
34
|
const { HttpServer, METHODS } = require('redweb');
|
|
@@ -33,12 +37,11 @@ const services = [
|
|
|
33
37
|
{
|
|
34
38
|
serviceName: '/submit-form',
|
|
35
39
|
method: METHODS.POST,
|
|
36
|
-
function: (req, res) => {
|
|
40
|
+
'function': (req, res) => {
|
|
37
41
|
const { name, email, message } = req.body;
|
|
38
42
|
if (!name || !email || !message) {
|
|
39
43
|
return res.status(400).json({ error: 'All fields are required' });
|
|
40
44
|
}
|
|
41
|
-
// Implement your logic here
|
|
42
45
|
res.status(200).json({ success: 'Form submitted successfully' });
|
|
43
46
|
}
|
|
44
47
|
}
|
|
@@ -46,11 +49,7 @@ const services = [
|
|
|
46
49
|
|
|
47
50
|
const options = {
|
|
48
51
|
port: 3000,
|
|
49
|
-
publicPaths: [
|
|
50
|
-
'./pages/my_public_html',
|
|
51
|
-
'./content/my_public_images',
|
|
52
|
-
'./styles/styles.css'
|
|
53
|
-
],
|
|
52
|
+
publicPaths: ['./public'],
|
|
54
53
|
encoding: 'urlencoded',
|
|
55
54
|
services: services
|
|
56
55
|
};
|
|
@@ -58,9 +57,7 @@ const options = {
|
|
|
58
57
|
const app = new HttpServer(options);
|
|
59
58
|
```
|
|
60
59
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
To create an HTTPS server, provide the SSL options including the paths to your SSL key and certificate files:
|
|
60
|
+
#### HTTPS Server
|
|
64
61
|
|
|
65
62
|
```javascript
|
|
66
63
|
const { HttpsServer } = require('redweb');
|
|
@@ -77,29 +74,31 @@ const options = {
|
|
|
77
74
|
const app = new HttpsServer(options);
|
|
78
75
|
```
|
|
79
76
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
To create a WebSocket server, use the `SocketServer` function:
|
|
77
|
+
#### WebSocket Server
|
|
83
78
|
|
|
84
79
|
```javascript
|
|
85
80
|
const { SocketServer } = require('redweb');
|
|
86
81
|
|
|
87
82
|
const options = {
|
|
88
83
|
port: 3000,
|
|
89
|
-
|
|
90
|
-
console.log('
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
84
|
+
connectionOpenCallback: (socket) => {
|
|
85
|
+
console.log('WebSocket client connected');
|
|
86
|
+
},
|
|
87
|
+
connectionCloseCallback: (socket) => {
|
|
88
|
+
console.log('WebSocket client disconnected');
|
|
89
|
+
},
|
|
90
|
+
messageHandlers: {
|
|
91
|
+
'msg': () => (socket, data) => {
|
|
92
|
+
console.log(data);
|
|
93
|
+
socket.send('guuuuuurl');
|
|
94
|
+
}
|
|
94
95
|
}
|
|
95
96
|
};
|
|
96
97
|
|
|
97
98
|
const socketServer = new SocketServer(options);
|
|
98
99
|
```
|
|
99
100
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
To create a secure WebSocket server, provide the SSL options:
|
|
101
|
+
#### Secure WebSocket Server
|
|
103
102
|
|
|
104
103
|
```javascript
|
|
105
104
|
const { SecureSocketServer } = require('redweb');
|
|
@@ -110,66 +109,70 @@ const options = {
|
|
|
110
109
|
key: './path/to/key.pem',
|
|
111
110
|
cert: './path/to/cert.pem'
|
|
112
111
|
},
|
|
113
|
-
|
|
114
|
-
console.log('
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
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
|
+
}
|
|
118
123
|
}
|
|
119
124
|
};
|
|
120
125
|
|
|
121
126
|
const secureSocketServer = new SecureSocketServer(options);
|
|
122
127
|
```
|
|
123
128
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
### HTTP/HTTPS Options
|
|
127
|
-
|
|
128
|
-
The `HttpServer` and `HttpsServer` constructors accept an options object with the following properties:
|
|
129
|
-
|
|
130
|
-
- **port**: The port number to bind the server (default: `80`).
|
|
131
|
-
- **bind**: The bind address for the server (default: `0.0.0.0`).
|
|
132
|
-
- **publicPaths**: An array of paths to serve static files from (default: `['./public']`).
|
|
133
|
-
- **services**: An array of services with their endpoints and handlers (default: `[]`).
|
|
134
|
-
- **listenCallback**: A callback function to execute once the server starts listening (default: `undefined`).
|
|
135
|
-
- **encoding**: The encoding type for the request bodies. It can be either `'json'` or `'urlencoded'` (default: `'json'`).
|
|
136
|
-
- **ssl**: SSL configuration for the HTTPS server. It should include `key` and `cert` paths.
|
|
137
|
-
|
|
138
|
-
### WebSocket/Secure WebSocket Options
|
|
129
|
+
### Managing Connected Clients
|
|
139
130
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
- **port**: The port number to bind the socket server (default: `3000`).
|
|
143
|
-
- **connectionCallback**: A callback function to execute once a client connects (default: `undefined`).
|
|
144
|
-
- **ssl**: SSL configuration for the secure WebSocket server. It should include `key` and `cert` paths.
|
|
145
|
-
|
|
146
|
-
### Example Options Object
|
|
131
|
+
RedWeb's WebSocket server maintains a list of connected clients by their IP addresses. This list is automatically updated when clients connect or disconnect.
|
|
147
132
|
|
|
148
133
|
```javascript
|
|
149
|
-
const
|
|
134
|
+
const { SocketServer } = require('redweb');
|
|
135
|
+
|
|
136
|
+
const socketServer = new SocketServer({
|
|
150
137
|
port: 3000,
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
138
|
+
connectionOpenCallback: (socket) => {
|
|
139
|
+
console.log('WebSocket client connected');
|
|
140
|
+
},
|
|
141
|
+
connectionCloseCallback: (socket) => {
|
|
142
|
+
console.log('WebSocket client disconnected');
|
|
143
|
+
},
|
|
144
|
+
messageHandlers: {
|
|
145
|
+
'msg': () => (socket, data) {
|
|
146
|
+
console.log(data);
|
|
147
|
+
socket.send('guuuuuurl');
|
|
160
148
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
// Access the list of connected clients
|
|
153
|
+
console.log(socketServer.clients); // Map of clients by their IP addresses
|
|
165
154
|
```
|
|
166
155
|
|
|
167
|
-
##
|
|
156
|
+
## Options
|
|
157
|
+
|
|
158
|
+
### HttpServer and HttpsServer Options
|
|
159
|
+
|
|
160
|
+
- **port**: Port number (default: `80`).
|
|
161
|
+
- **bind**: Bind address (default: `0.0.0.0`).
|
|
162
|
+
- **publicPaths**: Array of paths to serve static files (default: `['./public']`).
|
|
163
|
+
- **services**: Array of services with endpoints and handlers (default: `[]`).
|
|
164
|
+
- **listenCallback**: Function to execute once the server starts listening.
|
|
165
|
+
- **encoding**: Encoding type for request bodies (`'json'` or `'urlencoded'`).
|
|
166
|
+
- **ssl**: SSL configuration for HTTPS server (`{ key: './path/to/key.pem', cert: './path/to/cert.pem' }`).
|
|
168
167
|
|
|
169
|
-
|
|
168
|
+
### SocketServer and SecureSocketServer Options
|
|
170
169
|
|
|
171
|
-
- `
|
|
172
|
-
-
|
|
170
|
+
- **port**: Port number (default: `3000`).
|
|
171
|
+
- **connectionOpenCallback**: Function to execute once a client connects.
|
|
172
|
+
- **connectionCloseCallback**: Function to execute once a client disconnects.
|
|
173
|
+
- **messageCallback**: Function to execute for every message received.
|
|
174
|
+
- **messageHandlers**: Object containing message handlers based on message type.
|
|
175
|
+
- **ssl**: SSL configuration for SecureSocketServer (`{ key: './path/to/key.pem', cert: './path/to/cert.pem' }`).
|
|
173
176
|
|
|
174
177
|
## License
|
|
175
178
|
|
package/SocketServer.js
CHANGED
|
@@ -21,7 +21,7 @@ const SOCKET_OPTIONS = {
|
|
|
21
21
|
connectionCloseCallback: undefined,
|
|
22
22
|
messageCallback: undefined,
|
|
23
23
|
messageHandlers: {
|
|
24
|
-
'ping': function(data) {
|
|
24
|
+
'ping': function(socket, data) { socket.send(JSON.stringify({ type: 'pong' })); }
|
|
25
25
|
},
|
|
26
26
|
ssl: null
|
|
27
27
|
};
|
|
@@ -51,7 +51,7 @@ function BaseSocketServer(server, options = {}) {
|
|
|
51
51
|
const { type, data } = parsedMessage;
|
|
52
52
|
|
|
53
53
|
if (this.messageHandlers[type]) {
|
|
54
|
-
this.messageHandlers[type]
|
|
54
|
+
this.messageHandlers[type](socket, data);
|
|
55
55
|
}
|
|
56
56
|
} catch (error) {
|
|
57
57
|
console.error(`Error handling message from ${ip}:`, error);
|