redweb 0.2.5 → 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 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 a web server with customizable options, including serving static files, defining custom API endpoints, and integrating WebSocket communication.
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
- To get started with RedWeb, simply initialize your RedWeb instance with the default options:
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
- const app = new HttpServer();
28
+ // WebSocket server with default configuration
29
+ const socketServer = new SocketServer();
23
30
  ```
24
31
 
25
32
  ### Custom Configuration
26
33
 
27
- You can also configure RedWeb according to your needs:
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
- ### HTTPS Server
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
- ### WebSocket Server
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
- connectionCallback: (socket) => {
90
- console.log('New client connected');
91
- socket.on('message', (data) => {
92
- console.log('Message received:', data);
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
- ### Secure WebSocket Server
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
- connectionCallback: (socket) => {
114
- console.log('New client connected');
115
- socket.on('message', (data) => {
116
- console.log('Message received:', data);
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
- ## 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
132
+ ### Managing Connected Clients
139
133
 
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
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 options = {
137
+ const { SocketServer } = require('redweb');
138
+
139
+ const socketServer = new SocketServer({
150
140
  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
- }
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
- listenCallback: () => console.log('Server is running...'),
163
- encoding: 'json'
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
- ## Methods
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
- RedWeb provides a `METHODS` object that contains the HTTP methods you can use for defining services:
171
+ ### SocketServer and SecureSocketServer Options
170
172
 
171
- - `METHODS.POST` - 'post'
172
- - `METHODS.GET` - 'get'
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': 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.6",
4
4
  "description": "A way to quickly set up an express server",
5
5
  "main": "index.js",
6
6
  "scripts": {