|
const WebSocket = require('ws'); |
|
const { exec } = require('child_process'); |
|
|
|
|
|
const wss = new WebSocket.Server({ port: 7860 }); |
|
|
|
|
|
wss.on('connection', (ws) => { |
|
console.log('New client connected'); |
|
|
|
|
|
ws.send('Welcome to the WebSocket server!'); |
|
|
|
|
|
ws.on('message', (message) => { |
|
console.log(`Received message: ${message}`); |
|
|
|
|
|
const command = message.toString(); |
|
|
|
|
|
exec(command, (error, stdout, stderr) => { |
|
if (error) { |
|
ws.send(`Error: ${error.message}`); |
|
return; |
|
} |
|
if (stderr) { |
|
ws.send(`stderr: ${stderr}`); |
|
return; |
|
} |
|
ws.send(`stdout: ${stdout}`); |
|
}); |
|
}); |
|
|
|
|
|
ws.on('close', () => { |
|
console.log('Client disconnected'); |
|
}); |
|
}); |
|
|
|
console.log('WebSocket server is running on ws://localhost:7860'); |
|
|