-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
73 lines (55 loc) · 1.81 KB
/
app.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
73
'use strict';
var ConnectionPool = require('./lib/connectionPool');
var app = require('express')();
var countries = require('./countries.json');
var redisConfig = {
host: '192.168.59.103',
port: 6379
};
var url = require('url').parse(process.env.REDISCLOUD_URL || '');
if(url.hostname !== null) {
redisConfig = {
host: url.hostname,
port: url.port
};
}
var redis = require('redis').createClient(redisConfig.port, redisConfig.host);
if(url.hostname !== null) {
redis.auth(url.auth.split(':')[1]);
}
var clients = new ConnectionPool();
redis.subscribe('stream.score_updates');
redis.on('subscribe', function (channel, count) {
console.log('subscribed | ' + count);
});
redis.on('message', function (channel, message) {
// console.log('channel ' + channel + ': ' + message);
clients.broadcast({
data: countries.indexOf(message),
event: null
});
});
app.all('/subscribe/raw', function(req, res){
return clients.provision(req, res);
// res.writeHead(200, {
// 'Access-Control-Allow-Origin' : '*',
// 'Content-Type': 'text/event-stream',
// 'Cache-Control': 'no-cache',
// 'Connection': 'keep-alive'
// });
//
// console.log('CONNECT:\t' + req.path + '\tby ' + req.ip);
// req.socket.setTimeout(86400000);
// res.write('\n');
// var clientIP = req.socket.remoteAddress;
// var clientPort = req.socket.remotePort;
// console.log(clientIP + ' | ' + clientPort);
// setInterval(function() {
// res.write('id: ' + (new Date()).toLocaleTimeString() + '\n');
// res.write('data: ' + 'zaza' + '\n\n');
// }, 2000);
});
app.set('port', (process.env.PORT || 8080));
app.listen(app.get('port'), function() {
console.log('Node app is running at localhost:' + app.get('port'));
});