Update server.js
Browse files
server.js
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
const WebSocket = require('ws');
|
|
|
2 |
|
3 |
-
// Create a WebSocket server listening on port
|
4 |
const wss = new WebSocket.Server({ port: 7860 });
|
5 |
|
6 |
// Event listener for new connections
|
@@ -14,8 +15,18 @@ wss.on('connection', (ws) => {
|
|
14 |
ws.on('message', (message) => {
|
15 |
console.log(`Received message: ${message}`);
|
16 |
|
17 |
-
//
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
});
|
20 |
|
21 |
// Event listener for client disconnects
|
|
|
1 |
const WebSocket = require('ws');
|
2 |
+
const { exec } = require('child_process');
|
3 |
|
4 |
+
// Create a WebSocket server listening on port 7860
|
5 |
const wss = new WebSocket.Server({ port: 7860 });
|
6 |
|
7 |
// Event listener for new connections
|
|
|
15 |
ws.on('message', (message) => {
|
16 |
console.log(`Received message: ${message}`);
|
17 |
|
18 |
+
// Execute the command received from the client
|
19 |
+
exec(message, (error, stdout, stderr) => {
|
20 |
+
if (error) {
|
21 |
+
ws.send(`Error: ${error.message}`);
|
22 |
+
return;
|
23 |
+
}
|
24 |
+
if (stderr) {
|
25 |
+
ws.send(`stderr: ${stderr}`);
|
26 |
+
return;
|
27 |
+
}
|
28 |
+
ws.send(`stdout: ${stdout}`);
|
29 |
+
});
|
30 |
});
|
31 |
|
32 |
// Event listener for client disconnects
|