redweb 0.4.1 → 0.4.3
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/LICENSE +20 -20
- package/index.js +10 -10
- package/package.json +24 -24
- package/src/http/BaseHttpServer.js +60 -60
- package/src/http/HttpServer.js +14 -14
- package/src/http/HttpsServer.js +17 -17
- package/src/http/index.js +1 -1
- package/src/sslConfig.js +20 -20
- package/src/ws/BaseSocketServer.js +95 -81
- package/src/ws/SecureSocketServer.js +20 -20
- package/src/ws/SocketServer.js +18 -18
- package/src/ws/index.js +5 -5
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2024 Arkam Mazrui
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Arkam Mazrui
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
SOFTWARE.
|
package/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
const { HttpServer, HttpsServer, METHODS } = require('./src/http');
|
|
2
|
-
const { SocketServer, SecureSocketServer, SOCKET_OPTIONS } = require('./src/ws');
|
|
3
|
-
module.exports = {
|
|
4
|
-
SocketServer,
|
|
5
|
-
SecureSocketServer,
|
|
6
|
-
HttpServer,
|
|
7
|
-
HttpsServer,
|
|
8
|
-
SOCKET_OPTIONS,
|
|
9
|
-
METHODS
|
|
10
|
-
};
|
|
1
|
+
const { HttpServer, HttpsServer, METHODS } = require('./src/http');
|
|
2
|
+
const { SocketServer, SecureSocketServer, SOCKET_OPTIONS } = require('./src/ws');
|
|
3
|
+
module.exports = {
|
|
4
|
+
SocketServer,
|
|
5
|
+
SecureSocketServer,
|
|
6
|
+
HttpServer,
|
|
7
|
+
HttpsServer,
|
|
8
|
+
SOCKET_OPTIONS,
|
|
9
|
+
METHODS
|
|
10
|
+
};
|
package/package.json
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "redweb",
|
|
3
|
-
"version": "0.4.
|
|
4
|
-
"description": "A way to quickly set up an express server",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"test": "node tester.js"
|
|
8
|
-
},
|
|
9
|
-
"files": [
|
|
10
|
-
"src/*"
|
|
11
|
-
],
|
|
12
|
-
"keywords": [],
|
|
13
|
-
"author": "",
|
|
14
|
-
"license": "ISC",
|
|
15
|
-
"dependencies": {
|
|
16
|
-
"express": "^4.19.2",
|
|
17
|
-
"ws": "^8.17.0"
|
|
18
|
-
},
|
|
19
|
-
"devDependencies": {
|
|
20
|
-
"@types/express": "^4.17.21",
|
|
21
|
-
"@types/jest": "^29.5.12",
|
|
22
|
-
"jest": "^29.7.0"
|
|
23
|
-
}
|
|
24
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "redweb",
|
|
3
|
+
"version": "0.4.3",
|
|
4
|
+
"description": "A way to quickly set up an express server",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "node tester.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"src/*"
|
|
11
|
+
],
|
|
12
|
+
"keywords": [],
|
|
13
|
+
"author": "",
|
|
14
|
+
"license": "ISC",
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"express": "^4.19.2",
|
|
17
|
+
"ws": "^8.17.0"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/express": "^4.17.21",
|
|
21
|
+
"@types/jest": "^29.5.12",
|
|
22
|
+
"jest": "^29.7.0"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -1,60 +1,60 @@
|
|
|
1
|
-
const express = require('express');
|
|
2
|
-
const bodyParser = require('body-parser');
|
|
3
|
-
const path = require('path');
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @typedef {'json' | 'urlencoded'} RedWebEncoding
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* RedWeb options object.
|
|
11
|
-
* @typedef {Object} RedWebOptions
|
|
12
|
-
* @property {number} [port=80] - The port number to bind the server.
|
|
13
|
-
* @property {string} [bind='0.0.0.0'] - The bind address for the server.
|
|
14
|
-
* @property {string[]} [publicPaths=['./public']] - An array of paths to serve static files from.
|
|
15
|
-
* @property {Array<{serviceName: string, method: string, function: Function}>} [services=[]] - An array of services with their endpoints and handlers.
|
|
16
|
-
* @property {Function} [listenCallback] - Callback function to execute once the server starts listening.
|
|
17
|
-
* @property {RedWebEncoding} [encoding='json'] - The encoding type for the request bodies ('json' or 'urlencoded').
|
|
18
|
-
* @property {Object} [ssl] - SSL configuration for HTTPS server.
|
|
19
|
-
* @property {string} [ssl.key] - Path to the SSL key file.
|
|
20
|
-
* @property {string} [ssl.cert] - Path to the SSL certificate file.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
const ENCODINGS = { json: 'json', urlencoded: 'urlencoded' };
|
|
24
|
-
const HTTP_OPTIONS = {
|
|
25
|
-
port: 80,
|
|
26
|
-
bind: '0.0.0.0',
|
|
27
|
-
publicPaths: ['./public'],
|
|
28
|
-
services: [],
|
|
29
|
-
listenCallback: undefined,
|
|
30
|
-
encoding: ENCODINGS.json,
|
|
31
|
-
ssl: null
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Base HTTP Server
|
|
36
|
-
* @param {RedWebOptions} options - Configuration options for RedWeb.
|
|
37
|
-
* @return {Object} Express application instance.
|
|
38
|
-
*/
|
|
39
|
-
function BaseHttpServer(options = {}) {
|
|
40
|
-
this.options = { ...HTTP_OPTIONS, ...options };
|
|
41
|
-
this.app = express();
|
|
42
|
-
Object.assign(this, this.options);
|
|
43
|
-
|
|
44
|
-
// Middleware to parse request bodies based on the specified encoding
|
|
45
|
-
if (this.encoding === ENCODINGS.json) {
|
|
46
|
-
this.app.use(bodyParser.json());
|
|
47
|
-
} else if (this.encoding === ENCODINGS.urlencoded) {
|
|
48
|
-
this.app.use(bodyParser.urlencoded({ extended: true }));
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
this.services.forEach(service => this.app[service.method](service.serviceName, service.function));
|
|
52
|
-
this.publicPaths.forEach(public_path => this.app.use(express.static(path.join(process.cwd(), public_path))));
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
module.exports = {
|
|
56
|
-
BaseHttpServer,
|
|
57
|
-
ENCODINGS,
|
|
58
|
-
HTTP_OPTIONS,
|
|
59
|
-
METHODS: {GET: 'get', POST: 'post', PUT: 'put', DELETE: 'delete'}
|
|
60
|
-
};
|
|
1
|
+
const express = require('express');
|
|
2
|
+
const bodyParser = require('body-parser');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {'json' | 'urlencoded'} RedWebEncoding
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* RedWeb options object.
|
|
11
|
+
* @typedef {Object} RedWebOptions
|
|
12
|
+
* @property {number} [port=80] - The port number to bind the server.
|
|
13
|
+
* @property {string} [bind='0.0.0.0'] - The bind address for the server.
|
|
14
|
+
* @property {string[]} [publicPaths=['./public']] - An array of paths to serve static files from.
|
|
15
|
+
* @property {Array<{serviceName: string, method: string, function: Function}>} [services=[]] - An array of services with their endpoints and handlers.
|
|
16
|
+
* @property {Function} [listenCallback] - Callback function to execute once the server starts listening.
|
|
17
|
+
* @property {RedWebEncoding} [encoding='json'] - The encoding type for the request bodies ('json' or 'urlencoded').
|
|
18
|
+
* @property {Object} [ssl] - SSL configuration for HTTPS server.
|
|
19
|
+
* @property {string} [ssl.key] - Path to the SSL key file.
|
|
20
|
+
* @property {string} [ssl.cert] - Path to the SSL certificate file.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
const ENCODINGS = { json: 'json', urlencoded: 'urlencoded' };
|
|
24
|
+
const HTTP_OPTIONS = {
|
|
25
|
+
port: 80,
|
|
26
|
+
bind: '0.0.0.0',
|
|
27
|
+
publicPaths: ['./public'],
|
|
28
|
+
services: [],
|
|
29
|
+
listenCallback: undefined,
|
|
30
|
+
encoding: ENCODINGS.json,
|
|
31
|
+
ssl: null
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Base HTTP Server
|
|
36
|
+
* @param {RedWebOptions} options - Configuration options for RedWeb.
|
|
37
|
+
* @return {Object} Express application instance.
|
|
38
|
+
*/
|
|
39
|
+
function BaseHttpServer(options = {}) {
|
|
40
|
+
this.options = { ...HTTP_OPTIONS, ...options };
|
|
41
|
+
this.app = express();
|
|
42
|
+
Object.assign(this, this.options);
|
|
43
|
+
|
|
44
|
+
// Middleware to parse request bodies based on the specified encoding
|
|
45
|
+
if (this.encoding === ENCODINGS.json) {
|
|
46
|
+
this.app.use(bodyParser.json());
|
|
47
|
+
} else if (this.encoding === ENCODINGS.urlencoded) {
|
|
48
|
+
this.app.use(bodyParser.urlencoded({ extended: true }));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
this.services.forEach(service => this.app[service.method](service.serviceName, service.function));
|
|
52
|
+
this.publicPaths.forEach(public_path => this.app.use(express.static(path.join(process.cwd(), public_path))));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
module.exports = {
|
|
56
|
+
BaseHttpServer,
|
|
57
|
+
ENCODINGS,
|
|
58
|
+
HTTP_OPTIONS,
|
|
59
|
+
METHODS: {GET: 'get', POST: 'post', PUT: 'put', DELETE: 'delete'}
|
|
60
|
+
};
|
package/src/http/HttpServer.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
const { BaseHttpServer } = require('./BaseHttpServer');
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* HTTP Server
|
|
5
|
-
* @param {RedWebOptions} options - Configuration options for RedWeb.
|
|
6
|
-
* @return {Object} Express application instance.
|
|
7
|
-
*/
|
|
8
|
-
function HttpServer(options = {}) {
|
|
9
|
-
BaseHttpServer.call(this, options);
|
|
10
|
-
this.app.listen(this.port, this.listenCallback ? this.listenCallback : () => console.log(`RedWeb HttpServer listening on port ${this.port}`));
|
|
11
|
-
return this;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
module.exports = HttpServer;
|
|
1
|
+
const { BaseHttpServer } = require('./BaseHttpServer');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* HTTP Server
|
|
5
|
+
* @param {RedWebOptions} options - Configuration options for RedWeb.
|
|
6
|
+
* @return {Object} Express application instance.
|
|
7
|
+
*/
|
|
8
|
+
function HttpServer(options = {}) {
|
|
9
|
+
BaseHttpServer.call(this, options);
|
|
10
|
+
this.app.listen(this.port, this.listenCallback ? this.listenCallback : () => console.log(`RedWeb HttpServer listening on port ${this.port}`));
|
|
11
|
+
return this;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
module.exports = HttpServer;
|
package/src/http/HttpsServer.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
const https = require('https');
|
|
2
|
-
const { BaseHttpServer } = require('./BaseHttpServer');
|
|
3
|
-
const loadSslConfig = require('../sslConfig');
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* HTTPS Server
|
|
7
|
-
* @param {RedWebOptions} options - Configuration options for RedWeb.
|
|
8
|
-
* @return {Object} Express application instance.
|
|
9
|
-
*/
|
|
10
|
-
function HttpsServer(options = {}) {
|
|
11
|
-
BaseHttpServer.call(this, options);
|
|
12
|
-
const sslOptions = loadSslConfig(this.ssl);
|
|
13
|
-
https.createServer(sslOptions, this.app).listen(this.port, this.listenCallback ? this.listenCallback : () => console.log(`RedWeb HttpsServer listening on port ${this.port}`));
|
|
14
|
-
return this;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
module.exports = HttpsServer;
|
|
1
|
+
const https = require('https');
|
|
2
|
+
const { BaseHttpServer } = require('./BaseHttpServer');
|
|
3
|
+
const loadSslConfig = require('../sslConfig');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* HTTPS Server
|
|
7
|
+
* @param {RedWebOptions} options - Configuration options for RedWeb.
|
|
8
|
+
* @return {Object} Express application instance.
|
|
9
|
+
*/
|
|
10
|
+
function HttpsServer(options = {}) {
|
|
11
|
+
BaseHttpServer.call(this, options);
|
|
12
|
+
const sslOptions = loadSslConfig(this.ssl);
|
|
13
|
+
https.createServer(sslOptions, this.app).listen(this.port, this.listenCallback ? this.listenCallback : () => console.log(`RedWeb HttpsServer listening on port ${this.port}`));
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
module.exports = HttpsServer;
|
package/src/http/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const { METHODS } = require('./BaseHttpServer');
|
|
1
|
+
const { METHODS } = require('./BaseHttpServer');
|
|
2
2
|
module.exports = { HttpServer: require('./HttpServer'), HttpsServer: require('./HttpsServer'), METHODS}
|
package/src/sslConfig.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Load SSL configuration
|
|
5
|
-
* @param {Object} sslOptions - The SSL options object containing the paths to key and cert files.
|
|
6
|
-
* @param {string} sslOptions.key - Path to the SSL key file.
|
|
7
|
-
* @param {string} sslOptions.cert - Path to the SSL certificate file.
|
|
8
|
-
* @return {Object} - The loaded SSL options containing key and cert.
|
|
9
|
-
*/
|
|
10
|
-
function loadSslConfig(sslOptions) {
|
|
11
|
-
if (!sslOptions || !sslOptions.key || !sslOptions.cert) {
|
|
12
|
-
throw new Error('SSL key and certificate paths must be provided');
|
|
13
|
-
}
|
|
14
|
-
return {
|
|
15
|
-
key: fs.readFileSync(sslOptions.key),
|
|
16
|
-
cert: fs.readFileSync(sslOptions.cert)
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
module.exports = loadSslConfig;
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Load SSL configuration
|
|
5
|
+
* @param {Object} sslOptions - The SSL options object containing the paths to key and cert files.
|
|
6
|
+
* @param {string} sslOptions.key - Path to the SSL key file.
|
|
7
|
+
* @param {string} sslOptions.cert - Path to the SSL certificate file.
|
|
8
|
+
* @return {Object} - The loaded SSL options containing key and cert.
|
|
9
|
+
*/
|
|
10
|
+
function loadSslConfig(sslOptions) {
|
|
11
|
+
if (!sslOptions || !sslOptions.key || !sslOptions.cert) {
|
|
12
|
+
throw new Error('SSL key and certificate paths must be provided');
|
|
13
|
+
}
|
|
14
|
+
return {
|
|
15
|
+
key: fs.readFileSync(sslOptions.key),
|
|
16
|
+
cert: fs.readFileSync(sslOptions.cert)
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = loadSslConfig;
|
|
@@ -1,81 +1,95 @@
|
|
|
1
|
-
const WebSocket = require('ws');
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @typedef {Object} SocketServerOptions
|
|
5
|
-
* @property {number} [port=3000] - The port number to bind the socket server.
|
|
6
|
-
* @property {Function} [connectionOpenCallback] - Callback function to execute once a client connects.
|
|
7
|
-
* @property {Function} [connectionCloseCallback] - Callback function to execute once a client disconnects.
|
|
8
|
-
* @property {Function} [messageCallback] - Callback function to execute for every message received.
|
|
9
|
-
* @property {Object} [messageHandlers] - Object containing message handlers based on message type.
|
|
10
|
-
* @property {Object} [ssl] - SSL configuration for SecureSocketServer.
|
|
11
|
-
* @property {string} [ssl.key] - Path to the SSL key file.
|
|
12
|
-
* @property {string} [ssl.cert] - Path to the SSL certificate file.
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
const SOCKET_OPTIONS = {
|
|
16
|
-
port: 3000,
|
|
17
|
-
connectionOpenCallback: undefined,
|
|
18
|
-
connectionCloseCallback: undefined,
|
|
19
|
-
messageCallback: undefined,
|
|
20
|
-
messageHandlers: {
|
|
21
|
-
ping: (socket, data) => socket.send(JSON.stringify({ type: 'pong' }))
|
|
22
|
-
},
|
|
23
|
-
ssl: null
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
this.clients
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
this.clients.
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
|
|
1
|
+
const WebSocket = require('ws');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {Object} SocketServerOptions
|
|
5
|
+
* @property {number} [port=3000] - The port number to bind the socket server.
|
|
6
|
+
* @property {Function} [connectionOpenCallback] - Callback function to execute once a client connects.
|
|
7
|
+
* @property {Function} [connectionCloseCallback] - Callback function to execute once a client disconnects.
|
|
8
|
+
* @property {Function} [messageCallback] - Callback function to execute for every message received.
|
|
9
|
+
* @property {Object} [messageHandlers] - Object containing message handlers based on message type.
|
|
10
|
+
* @property {Object} [ssl] - SSL configuration for SecureSocketServer.
|
|
11
|
+
* @property {string} [ssl.key] - Path to the SSL key file.
|
|
12
|
+
* @property {string} [ssl.cert] - Path to the SSL certificate file.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const SOCKET_OPTIONS = {
|
|
16
|
+
port: 3000,
|
|
17
|
+
connectionOpenCallback: undefined,
|
|
18
|
+
connectionCloseCallback: undefined,
|
|
19
|
+
messageCallback: undefined,
|
|
20
|
+
messageHandlers: {
|
|
21
|
+
ping: (socket, data) => socket.send(JSON.stringify({ type: 'pong' }))
|
|
22
|
+
},
|
|
23
|
+
ssl: null
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
*
|
|
28
|
+
* @param {BaseSocketServer} socketServer
|
|
29
|
+
* @param {Object} message
|
|
30
|
+
*/
|
|
31
|
+
function broadcast(socketServer, message) {
|
|
32
|
+
socketServer.clients.forEach(client => client.send(JSON.stringify(message)));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
class BaseSocketServer {
|
|
36
|
+
/**
|
|
37
|
+
* Base Socket Server
|
|
38
|
+
* @param {Object} server - The HTTP or HTTPS server instance.
|
|
39
|
+
* @param {SocketServerOptions} options - Configuration options for SocketServer.
|
|
40
|
+
*/
|
|
41
|
+
constructor(server, options = {}) {
|
|
42
|
+
this.wss = new WebSocket.Server({ server });
|
|
43
|
+
this.clients = new Map(); // Use a Map to store clients by their IP addresses
|
|
44
|
+
Object.assign(this, { ...SOCKET_OPTIONS, ...options });
|
|
45
|
+
|
|
46
|
+
this.wss.on('connection', this.handleConnection.bind(this));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
handleConnection(socket, req) {
|
|
50
|
+
const ip = req.socket.remoteAddress;
|
|
51
|
+
console.log(`New client connected: ${ip}`);
|
|
52
|
+
this.clients.set(ip, socket);
|
|
53
|
+
|
|
54
|
+
if (this.connectionOpenCallback) this.connectionOpenCallback(socket);
|
|
55
|
+
|
|
56
|
+
socket.on('message', (message) => this.handleMessage(socket, message, ip));
|
|
57
|
+
socket.on('close', () => this.handleClose(socket, ip));
|
|
58
|
+
socket.on('error', (error) => this.handleError(socket, error, ip));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
handleMessage(socket, message, ip) {
|
|
62
|
+
try {
|
|
63
|
+
console.info(`Received message from ${ip}: ${message}`);
|
|
64
|
+
if (this.messageCallback) this.messageCallback(socket, message);
|
|
65
|
+
|
|
66
|
+
const { type, data } = JSON.parse(message);
|
|
67
|
+
if (this.messageHandlers[type]) {
|
|
68
|
+
this.messageHandlers[type](socket, data);
|
|
69
|
+
}
|
|
70
|
+
} catch (error) {
|
|
71
|
+
console.error(`Error handling message from ${ip}:`, error);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
handleClose(socket, ip) {
|
|
76
|
+
console.log(`Client disconnected: ${ip}`);
|
|
77
|
+
this.clients.delete(ip);
|
|
78
|
+
if (this.connectionCloseCallback) this.connectionCloseCallback(socket);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
handleError(socket, error, ip) {
|
|
82
|
+
console.error(`Socket error from ${ip}:`, error);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
*
|
|
87
|
+
* This sends the same message to all clients.
|
|
88
|
+
* @param {Object} message
|
|
89
|
+
*/
|
|
90
|
+
broadcast(message) {
|
|
91
|
+
broadcast(this, message);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
module.exports = {BaseSocketServer, SOCKET_OPTIONS};
|
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
const https = require('https');
|
|
2
|
-
const loadSslConfig = require('../sslConfig');
|
|
3
|
-
const { BaseSocketServer } = require('./
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Secure WebSocket Server
|
|
7
|
-
* @param {SocketServerOptions} options - Configuration options for SecureSocketServer.
|
|
8
|
-
* @return {Object} WebSocket server instance.
|
|
9
|
-
*/
|
|
10
|
-
class SecureSocketServer extends BaseSocketServer {
|
|
11
|
-
constructor(options = {}) {
|
|
12
|
-
const sslOptions = loadSslConfig(options.ssl);
|
|
13
|
-
const server = https.createServer(sslOptions);
|
|
14
|
-
super(server, options);
|
|
15
|
-
server.listen(this.port, () => console.log(`RedWeb SecureSocketServer listening on port ${this.port}`));
|
|
16
|
-
return this;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
module.exports = SecureSocketServer;
|
|
1
|
+
const https = require('https');
|
|
2
|
+
const loadSslConfig = require('../sslConfig');
|
|
3
|
+
const { BaseSocketServer } = require('./BaseSocketServer');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Secure WebSocket Server
|
|
7
|
+
* @param {SocketServerOptions} options - Configuration options for SecureSocketServer.
|
|
8
|
+
* @return {Object} WebSocket server instance.
|
|
9
|
+
*/
|
|
10
|
+
class SecureSocketServer extends BaseSocketServer {
|
|
11
|
+
constructor(options = {}) {
|
|
12
|
+
const sslOptions = loadSslConfig(options.ssl);
|
|
13
|
+
const server = https.createServer(sslOptions);
|
|
14
|
+
super(server, options);
|
|
15
|
+
server.listen(this.port, () => console.log(`RedWeb SecureSocketServer listening on port ${this.port}`));
|
|
16
|
+
return this;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = SecureSocketServer;
|
package/src/ws/SocketServer.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
const http = require('http');
|
|
2
|
-
const { BaseSocketServer } = require('./
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* WebSocket Server
|
|
6
|
-
* @param {SocketServerOptions} options - Configuration options for SocketServer.
|
|
7
|
-
* @return {Object} WebSocket server instance.
|
|
8
|
-
*/
|
|
9
|
-
class SocketServer extends BaseSocketServer {
|
|
10
|
-
constructor(options = {}) {
|
|
11
|
-
const server = http.createServer();
|
|
12
|
-
super(server, options);
|
|
13
|
-
server.listen(this.port, () => console.log(`RedWeb SocketServer listening on port ${this.port}`));
|
|
14
|
-
return this;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
module.exports = SocketServer;
|
|
1
|
+
const http = require('http');
|
|
2
|
+
const { BaseSocketServer } = require('./BaseSocketServer');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* WebSocket Server
|
|
6
|
+
* @param {SocketServerOptions} options - Configuration options for SocketServer.
|
|
7
|
+
* @return {Object} WebSocket server instance.
|
|
8
|
+
*/
|
|
9
|
+
class SocketServer extends BaseSocketServer {
|
|
10
|
+
constructor(options = {}) {
|
|
11
|
+
const server = http.createServer();
|
|
12
|
+
super(server, options);
|
|
13
|
+
server.listen(this.port, () => console.log(`RedWeb SocketServer listening on port ${this.port}`));
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
module.exports = SocketServer;
|
package/src/ws/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
const { SOCKET_OPTIONS } = require('./BaseSocketServer');
|
|
2
|
-
module.exports = {
|
|
3
|
-
SecureSocketServer: require('./SecureSocketServer'),
|
|
4
|
-
SocketServer: require('./SocketServer'),
|
|
5
|
-
SOCKET_OPTIONS
|
|
1
|
+
const { SOCKET_OPTIONS } = require('./BaseSocketServer');
|
|
2
|
+
module.exports = {
|
|
3
|
+
SecureSocketServer: require('./SecureSocketServer'),
|
|
4
|
+
SocketServer: require('./SocketServer'),
|
|
5
|
+
SOCKET_OPTIONS
|
|
6
6
|
}
|