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 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 a web server with customizable options, including serving static files, defining custom API endpoints, and integrating WebSocket communication.
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
- To get started with RedWeb, simply initialize your RedWeb instance with the default options:
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
- const app = new HttpServer();
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
- You can also configure RedWeb according to your needs:
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
- ### HTTPS Server
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
- ### WebSocket Server
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
- connectionCallback: (socket) => {
90
- console.log('New client connected');
91
- socket.on('message', (data) => {
92
- console.log('Message received:', data);
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
- ### Secure WebSocket Server
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
- connectionCallback: (socket) => {
114
- console.log('New client connected');
115
- socket.on('message', (data) => {
116
- console.log('Message received:', data);
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
- ## Options
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
- The `SocketServer` and `SecureSocketServer` constructors accept an options object with the following properties:
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 options = {
134
+ const { SocketServer } = require('redweb');
135
+
136
+ const socketServer = new SocketServer({
150
137
  port: 3000,
151
- bind: '127.0.0.1',
152
- publicPaths: ['./public', './assets'],
153
- services: [
154
- {
155
- serviceName: '/api/data',
156
- method: METHODS.GET,
157
- function: (req, res) => {
158
- res.json({ message: 'Hello, world!' });
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
- listenCallback: () => console.log('Server is running...'),
163
- encoding: 'json'
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
- ## Methods
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
- RedWeb provides a `METHODS` object that contains the HTTP methods you can use for defining services:
168
+ ### SocketServer and SecureSocketServer Options
170
169
 
171
- - `METHODS.POST` - 'post'
172
- - `METHODS.GET` - 'get'
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) { this.send(JSON.stringify({ type: 'pong' })); }
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].call(socket, data);
54
+ this.messageHandlers[type](socket, data);
55
55
  }
56
56
  } catch (error) {
57
57
  console.error(`Error handling message from ${ip}:`, error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "redweb",
3
- "version": "0.2.5",
3
+ "version": "0.2.7",
4
4
  "description": "A way to quickly set up an express server",
5
5
  "main": "index.js",
6
6
  "scripts": {