Update server.js
Browse files
server.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
const WebSocket = require('ws');
|
| 2 |
-
const {
|
| 3 |
|
| 4 |
// Create a WebSocket server listening on port 7860
|
| 5 |
const wss = new WebSocket.Server({ port: 7860 });
|
|
@@ -18,24 +18,30 @@ wss.on('connection', (ws) => {
|
|
| 18 |
// Convert message (Buffer) to string
|
| 19 |
const command = message.toString();
|
| 20 |
|
| 21 |
-
//
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
});
|
| 40 |
});
|
| 41 |
|
|
@@ -45,4 +51,4 @@ wss.on('connection', (ws) => {
|
|
| 45 |
});
|
| 46 |
});
|
| 47 |
|
| 48 |
-
console.log('WebSocket server is running on ws://localhost:7860');
|
|
|
|
| 1 |
const WebSocket = require('ws');
|
| 2 |
+
const { spawn } = require('child_process');
|
| 3 |
|
| 4 |
// Create a WebSocket server listening on port 7860
|
| 5 |
const wss = new WebSocket.Server({ port: 7860 });
|
|
|
|
| 18 |
// Convert message (Buffer) to string
|
| 19 |
const command = message.toString();
|
| 20 |
|
| 21 |
+
// Split command into executable and arguments
|
| 22 |
+
const [cmd, ...args] = command.split(' ');
|
| 23 |
+
|
| 24 |
+
// Spawn a new process to execute the command
|
| 25 |
+
const child = spawn(cmd, args, { shell: true });
|
| 26 |
+
|
| 27 |
+
// Send stdout and stderr to the client
|
| 28 |
+
child.stdout.on('data', (data) => {
|
| 29 |
+
ws.send(`stdout: ${data}`);
|
| 30 |
+
console.log(`stdout: ${data}`);
|
| 31 |
+
});
|
| 32 |
+
|
| 33 |
+
child.stderr.on('data', (data) => {
|
| 34 |
+
ws.send(`stderr: ${data}`);
|
| 35 |
+
console.log(`stderr: ${data}`);
|
| 36 |
+
});
|
| 37 |
+
|
| 38 |
+
child.on('error', (error) => {
|
| 39 |
+
ws.send(`Error: ${error.message}`);
|
| 40 |
+
console.log(`Error: ${error.message}`);
|
| 41 |
+
});
|
| 42 |
+
|
| 43 |
+
child.on('close', (code) => {
|
| 44 |
+
console.log(`Child process exited with code ${code}`);
|
| 45 |
});
|
| 46 |
});
|
| 47 |
|
|
|
|
| 51 |
});
|
| 52 |
});
|
| 53 |
|
| 54 |
+
console.log('WebSocket server is running on ws://localhost:7860');
|