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