-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
73 lines (54 loc) · 1.88 KB
/
server.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
60
61
62
63
64
65
66
67
68
69
70
71
72
var WebSocketServer = require('ws').Server
, http = require('http')
, express = require('express')
, app = express()
, port = process.env.PORT || 5000;
app.use(express.static(__dirname + '/'));
var server = http.createServer(app);
server.listen(port);
console.log('http server listening on %d', port);
var mapper = require('./server/mapper.js');
var wss = new WebSocketServer({server: server});
console.log('websocket server created');
wss.on('connection', function(ws) {
function newClient() {
var numberOfUpdatesMade = 0;
function getEmailsAndUpdateClients() {
numberOfUpdatesMade ++;
// if (numberOfUpdatesMade < 5) {
console.log('checking for updates (' + numberOfUpdatesMade + ')');
var currentData = mapper.readEmail(function(emailData, changes) {
if(changes || numberOfUpdatesMade <= 2) {
console.log('CHANGES!');
ws.send(JSON.stringify(emailData), function() { });
} else {
console.log('no changes');
}
});
// }
}
getEmailsAndUpdateClients();
var id = setInterval(getEmailsAndUpdateClients, 5000);
return id;
}
var clientId = newClient();
console.log('websocket connection open');
ws.on('close', function() {
console.log('websocket connection close');
clearInterval(clientId);
});
});
GLOBAL.TODAY_EXTERNAL = 20;
GLOBAL.TODAY_INTERNAL = 20;
app.get('/fake/today/external/:num',
function(req, res) {
console.log('faked it', req.params.num);
GLOBAL.TODAY_EXTERNAL = req.params.num;
res.send('Set external emails today to ' + req.params.num);
});
app.get('/fake/today/internal/:num',
function(req, res) {
console.log('faked it', req.params.num);
GLOBAL.TODAY_INTERNAL = req.params.num;
res.send('Set internal emails today to ' + req.params.num);
});