-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
90 lines (75 loc) · 2.56 KB
/
server.js
File metadata and controls
90 lines (75 loc) · 2.56 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const path = require('path');
const fs = require('fs');
const uuid = require('uuid/v4');
const { spawn } = require('child_process');
app.use(express.static(path.join(__dirname, 'public')));
io.on('connection', function(socket){
console.log('a user connected');
});
http.listen(12345, function(){
console.log('listening on *:12345');
});
var children = {};
function setup_child(socket, msg) {
var child = spawn('python3', ['shim.py'], {stdio: 'pipe'});
// TODO: make sure this msg is valid?
child.stdin.write(JSON.stringify(msg) + '\n')
children[socket.id] = child;
//child.stdout.setEncoding('utf8')
child.stdout.on('data', function(data) {
console.log(data.toString())
// don't bother parsing it if the child is going to have to do that again anyway
socket.emit('info', data.toString())
})
// TODO: handle this
child.stderr.on('data', function(data) {
console.log(data.toString())
})
// TODO: do we always output stderr before stdout?
child.on('close', function(code, signal) {
console.log('closing code: ' + code + ' ' + signal)
socket.emit('shim-exit', {
'return_code': code
});
delete children[socket.id];
})
child.on('exit', function(code, signal) {
console.log('exiting: ' + code + ' ' + signal)
socket.emit('shim-exit', {
'return_code': code
});
delete children[socket.id];
})
}
// TODO: on disconnect, delete child process?
// TODO: if shim dies, we seem to, too
io.on('connection', function(socket){
socket.on('disconnect', function() {
console.log('user disconnected');
var child = children[socket.id]
// TODO: give it a minute to reconnect before killing it?? Is reconnecting possible?
if (typeof child !== 'undefined') {
child.kill('SIGKILL');
}
});
socket.on('cmd', function(msg){
var child = children[socket.id]
if (typeof child !== 'undefined') {
child.stdin.write(JSON.stringify(msg) + '\n')
} else {
socket.emit('shim-not-running', "Child not running!");
}
});
// TODO: set a timeout so they can't take forever running code?
socket.on('code', function(msg){
var child = children[socket.id]
if (typeof child !== 'undefined') {
child.kill('SIGKILL');
}
setup_child(socket, msg);
});
});