-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (49 loc) · 1.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const commands = require('./commands.json').commands;
const path = require('path');
const express = require('express');
const WebSocket = require('ws');
const { spawn } = require('child_process');
const os = require('os');
const app = express();
app.use(express.static('public'));
const server = require('http').createServer(app);
const wss = new WebSocket.Server({ server });
wss.on('connection', ws => {
// handle WebSocket connection here
console.log('WebSocket connected');
});
server.listen(3333, () => {
console.log('Server is listening on port 3333');
});
// start relayer
commands.forEach(command => {
let process;
const isWindows = os.platform() === "win32";
if (isWindows) {
process = spawn('wsl', [command.path, ...command.arguments]);
}
else {
process = spawn(command.path, command.arguments);
}
// process stdout handler
process.stdout.on('data', (data) => {
console.log(data.toString());
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({ stdout: data.toString(), info: command.info }));
}
});
});
// process stderr handler
process.stderr.on('data', (data) => {
console.log(data.toString());
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({ stderr: data.toString(), info: command.info }));
}
});
});
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, '/public/index.html'));
});