-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (50 loc) · 1.64 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
var express = require('express'),
app = express(),
bodyParser = require('body-parser'),
socketio = require('socket.io'),
_ = require('lodash'),
jobs = require('./lib/server/jobs'),
history = require('./lib/server/history');
history.load().then(function () {
var server = app.listen(3141, function () {
console.log('Mockboard listening on port 3141');
});
var io = socketio.listen(server);
io.on('connection', function (socket) {
// send latest data to widget on connection
socket.on('subscribe', function (channel) {
var currentHistory = history.getHistory();
if (_.has(currentHistory, channel)) {
socket.emit(channel, currentHistory[channel]);
}
});
});
/**
* send data to widgets
* adds updatedAt value with current time
* updates history
*
* @param {String} event - Event name
* @param {Object} data - Event data
*/
function sendData(event, data) {
data.updatedAt = Math.round(new Date().getTime() / 1000.0);
history.updateHistory(event, data);
io.emit(event, data);
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use('/assets', express.static(__dirname + '/lib/client/dist/assets'));
// api endpoint for custom widget data updates
app.post('/api/widgets/:event', function (req, res) {
var event = req.params.event;
var body = req.body;
sendData(event, body);
res.sendStatus(204);
});
app.all('*', function (req, res) {
res.sendFile('index.html', { root: __dirname + '/lib/client/dist' });
});
jobs.registerEmit(sendData);
jobs.scheduleJobs();
});