redweb 0.1.7 → 0.1.9
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/SocketServer.js +12 -12
- package/package.json +2 -2
package/SocketServer.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const http = require('http');
|
|
2
2
|
const https = require('https');
|
|
3
|
-
const
|
|
3
|
+
const WebSocket = require('ws');
|
|
4
4
|
const loadSslConfig = require('./sslConfig');
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -21,7 +21,7 @@ const SOCKET_OPTIONS = {
|
|
|
21
21
|
connectionCloseCallback: undefined,
|
|
22
22
|
messageCallback: undefined,
|
|
23
23
|
messageHandlers: {
|
|
24
|
-
'ping': (socket) => socket.send('pong')
|
|
24
|
+
'ping': (socket) => socket.send(JSON.stringify({ type: 'pong' }))
|
|
25
25
|
},
|
|
26
26
|
ssl: null
|
|
27
27
|
};
|
|
@@ -30,32 +30,30 @@ const SOCKET_OPTIONS = {
|
|
|
30
30
|
* Base Socket Server
|
|
31
31
|
* @param {Object} server - The HTTP or HTTPS server instance.
|
|
32
32
|
* @param {SocketServerOptions} options - Configuration options for SocketServer.
|
|
33
|
-
* @return {Object} Socket.IO server instance.
|
|
34
33
|
*/
|
|
35
34
|
function BaseSocketServer(server, options = {}) {
|
|
36
|
-
this.
|
|
35
|
+
this.wss = new WebSocket.Server({ server });
|
|
37
36
|
Object.assign(this, { ...SOCKET_OPTIONS, ...options });
|
|
38
37
|
|
|
39
|
-
this.
|
|
38
|
+
this.wss.on('connection', (socket) => {
|
|
40
39
|
console.log('New client connected');
|
|
41
40
|
if (this.connectionOpenCallback) this.connectionOpenCallback(socket);
|
|
42
41
|
|
|
43
42
|
socket.on('message', (message) => {
|
|
44
43
|
try {
|
|
44
|
+
if (this.messageCallback) this.messageCallback(socket, message);
|
|
45
45
|
const parsedMessage = JSON.parse(message);
|
|
46
46
|
const { type, data } = parsedMessage;
|
|
47
47
|
|
|
48
48
|
if (this.messageHandlers[type]) {
|
|
49
49
|
this.messageHandlers[type](socket, data);
|
|
50
50
|
}
|
|
51
|
-
|
|
52
|
-
if (this.messageCallback) this.messageCallback(socket, message);
|
|
53
51
|
} catch (error) {
|
|
54
52
|
console.error('Error handling message:', error);
|
|
55
53
|
}
|
|
56
54
|
});
|
|
57
55
|
|
|
58
|
-
socket.on('
|
|
56
|
+
socket.on('close', () => {
|
|
59
57
|
console.log('Client disconnected');
|
|
60
58
|
if (this.connectionCloseCallback) this.connectionCloseCallback(socket);
|
|
61
59
|
});
|
|
@@ -64,31 +62,33 @@ function BaseSocketServer(server, options = {}) {
|
|
|
64
62
|
console.error('Socket error:', error);
|
|
65
63
|
});
|
|
66
64
|
});
|
|
65
|
+
|
|
66
|
+
return this;
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
/**
|
|
70
70
|
* WebSocket Server
|
|
71
71
|
* @param {SocketServerOptions} options - Configuration options for SocketServer.
|
|
72
|
-
* @return {Object}
|
|
72
|
+
* @return {Object} WebSocket server instance.
|
|
73
73
|
*/
|
|
74
74
|
function SocketServer(options = {}) {
|
|
75
75
|
const server = http.createServer();
|
|
76
76
|
BaseSocketServer.call(this, server, options);
|
|
77
77
|
server.listen(this.port, () => console.log(`RedWeb SocketServer listening on port ${this.port}`));
|
|
78
|
-
return this
|
|
78
|
+
return this;
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
/**
|
|
82
82
|
* Secure WebSocket Server
|
|
83
83
|
* @param {SocketServerOptions} options - Configuration options for SecureSocketServer.
|
|
84
|
-
* @return {Object}
|
|
84
|
+
* @return {Object} WebSocket server instance.
|
|
85
85
|
*/
|
|
86
86
|
function SecureSocketServer(options = {}) {
|
|
87
87
|
const sslOptions = loadSslConfig(options.ssl);
|
|
88
88
|
const server = https.createServer(sslOptions);
|
|
89
89
|
BaseSocketServer.call(this, server, options);
|
|
90
90
|
server.listen(this.port, () => console.log(`RedWeb SecureSocketServer listening on port ${this.port}`));
|
|
91
|
-
return this
|
|
91
|
+
return this;
|
|
92
92
|
}
|
|
93
93
|
|
|
94
94
|
module.exports = {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "redweb",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
4
4
|
"description": "A way to quickly set up an express server",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"license": "ISC",
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"express": "^4.19.2",
|
|
20
|
-
"
|
|
20
|
+
"ws": "^8.17.0"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@types/express": "^4.17.21",
|