NEWTESTD / server.js
clone3's picture
Create server.js
49b9e4a verified
raw
history blame contribute delete
701 Bytes
const WebSocket = require('ws');
const os = require('os');
const pty = require('node-pty');
// Create WebSocket server
const wss = new WebSocket.Server({ port: 6060 });
wss.on('connection', function connection(ws) {
// Determine the shell to use based on the OS
const shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash';
// Spawn a terminal instance
const ptyProcess = pty.spawn(shell, [], {
name: 'xterm-color',
env: process.env,
});
// Send command to the terminal
ws.on('message', (command) => {
ptyProcess.write(command + '\n');
});
// Send terminal output to the WebSocket client
ptyProcess.on('data', function (data) {
ws.send(data);
});
});