initial commit
Change-Id: Ia748646eaf5c18f44eb5b147ff577d0d5ee844e8
This commit is contained in:
77
result/server.js
Normal file
77
result/server.js
Normal file
@@ -0,0 +1,77 @@
|
||||
var express = require('express'),
|
||||
async = require('async'),
|
||||
{ Pool } = require('pg'),
|
||||
cookieParser = require('cookie-parser'),
|
||||
app = express(),
|
||||
server = require('http').Server(app),
|
||||
io = require('socket.io')(server);
|
||||
|
||||
var port = process.env.PORT || 4000;
|
||||
|
||||
io.on('connection', function (socket) {
|
||||
|
||||
socket.emit('message', { text : 'Welcome!' });
|
||||
|
||||
socket.on('subscribe', function (data) {
|
||||
socket.join(data.channel);
|
||||
});
|
||||
});
|
||||
|
||||
var pool = new Pool({
|
||||
connectionString: 'postgres://postgres:postgres@db/postgres'
|
||||
});
|
||||
|
||||
async.retry(
|
||||
{times: 1000, interval: 1000},
|
||||
function(callback) {
|
||||
pool.connect(function(err, client, done) {
|
||||
if (err) {
|
||||
console.error("Waiting for db");
|
||||
}
|
||||
callback(err, client);
|
||||
});
|
||||
},
|
||||
function(err, client) {
|
||||
if (err) {
|
||||
return console.error("Giving up");
|
||||
}
|
||||
console.log("Connected to db");
|
||||
getVotes(client);
|
||||
}
|
||||
);
|
||||
|
||||
function getVotes(client) {
|
||||
client.query('SELECT vote, COUNT(id) AS count FROM votes GROUP BY vote', [], function(err, result) {
|
||||
if (err) {
|
||||
console.error("Error performing query: " + err);
|
||||
} else {
|
||||
var votes = collectVotesFromResult(result);
|
||||
io.sockets.emit("scores", JSON.stringify(votes));
|
||||
}
|
||||
|
||||
setTimeout(function() {getVotes(client) }, 1000);
|
||||
});
|
||||
}
|
||||
|
||||
function collectVotesFromResult(result) {
|
||||
var votes = {a: 0, b: 0};
|
||||
|
||||
result.rows.forEach(function (row) {
|
||||
votes[row.vote] = parseInt(row.count);
|
||||
});
|
||||
|
||||
return votes;
|
||||
}
|
||||
|
||||
app.use(cookieParser());
|
||||
app.use(express.urlencoded());
|
||||
app.use(express.static(__dirname + '/views'));
|
||||
|
||||
app.get('/', function (req, res) {
|
||||
res.sendFile(path.resolve(__dirname + '/views/index.html'));
|
||||
});
|
||||
|
||||
server.listen(port, function () {
|
||||
var port = server.address().port;
|
||||
console.log('App running on port ' + port);
|
||||
});
|
||||
Reference in New Issue
Block a user