-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (41 loc) · 1.19 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
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var argv = require('yargs').argv;
var startServer = argv['server'] !== false;
var doLogging = argv['logging'] !== false;
var simulation = require('./simulation');
if (startServer) {
// start a server
var PORT = 3000;
server.listen(PORT);
console.log('Server listening on http://localhost:' + PORT);
app.use('/', express.static(__dirname + '/client'));
app.use('/node_modules/', express.static(__dirname + '/node_modules'));
function broadcast (event, data) {
// emit to all connected clients
var connections = io.sockets.connected;
for (var id in connections) {
if (connections.hasOwnProperty(id)) {
connections[id].emit(event, data);
}
}
}
io.on('connection', function (socket) {
// emit all logs from history
socket.emit('logs', simulation.logs());
});
simulation.on('log', function (log) {
broadcast('log', log);
});
}
else {
// log to the console
if (doLogging) {
simulation.on('log', function (log) {
console.log('log', JSON.stringify(log));
});
}
}
simulation.start(argv);