-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
64 lines (55 loc) · 1.47 KB
/
server.js
File metadata and controls
64 lines (55 loc) · 1.47 KB
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
60
61
62
63
64
import { WebSocketServer } from 'ws';
import { spawn } from 'child_process';
import { config } from './config'
const FOXDOT_PATH = '/path/to/FoxDot';
// Start FoxDot
const foxdot = spawn('python', ['-m', 'FoxDot', '-p'], {
cwd: config.FOXDOT_PATH,
env: {...process.env, PYTHONUNBUFFERED: '1'}
});
console.log(`FoxDot started, pid: ${foxdot.pid} `);
// WebSocket backend Server
const wss = new WebSocketServer({ port: 1234 });
wss.on('connection', (ws, req) => {
console.log('New client connected');
//Foxdot Message
ws.on('message', async (message) => {
try {
const data = JSON.parse(message.toString());
if (data.type === 'evaluate_code') {
const {code} = data;
broadcastLog(`>> ${code}`);
foxdot.stdin.write(data.code + '\n' + '\n');
}
} catch (e) {
// Ignore non-JSON messages
}
});
ws.on('close', () => {
console.log('Client disconnected');
});
});
// Logs FoxDot
foxdot.stdout.on('data', (data) => {
try {
const logMessage = data.toString();
console.log(logMessage);
broadcastLog(logMessage);
} catch (e) {
console.error('error sending log message:', e);
}
});
// Logs Foxdot console
function broadcastLog(message, color=null) {
const messageObj = {
type: 'foxdot_log',
data: message,
color: color
};
wss.clients.forEach(client => {
if (client.readyState === 1) {
client.send(JSON.stringify(messageObj));
}
});
}
console.log('Server started port 1234');