-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
55 lines (48 loc) · 1.51 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
const STATIC_PORT = 8080;
const SOCKET_PORT = 8081;
const ANIM_DELAY = 50;
commandq = [];
inpEof = false;
var stdin = process.openStdin();
stdin.addListener("data", function(d) {
var inp = d.toString();
var lines = inp.split('\n');
lines = lines.filter(function(line) { return line.length > 0;});
for (var lineNo = 0; lineNo < lines.length; lineNo++) {
var plottyRegex = /.*<plotty:(.*)>.*/g;
var plottyCommand = plottyRegex.exec(lines[lineNo]);
if (plottyCommand != null) {
plottyCommand = plottyCommand[1]
command = plottyCommand.split(",");
command = command.map(function(e) {return e.trim()});
commandq.push(command);
} else {
console.log(lines[lineNo]);
}
}
}).addListener("end", function() {
inpEof = true;
});
// create static web server
var connect = require('connect');
var serveStatic = require('serve-static');
connect().use(serveStatic(__dirname)).listen(STATIC_PORT, function(){
console.log('Server running on ' + STATIC_PORT + '...');
});
// create Websocket server
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: SOCKET_PORT });
wss.on('connection', function connection(ws) {
console.log("connected");
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
setInterval(function() {
if (commandq.length > 0) {
ws.send(JSON.stringify(commandq[0]));
commandq.splice(0, 1);
} else if (inpEof) {
process.exit();
}
}, (ANIM_DELAY / Math.log(commandq.length)));
});