-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
66 lines (56 loc) · 1.81 KB
/
app.js
File metadata and controls
66 lines (56 loc) · 1.81 KB
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
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
var app = require('express')(),
server = require('http').Server(app);
io = require('socket.io')(server);
config = require('./config/config'),
glob = require('glob'),
mongoose = require('mongoose');
mongoose.connect(config.db);
var db = mongoose.connection;
db.on('error', function () {
throw new Error('unable to connect to database at ' + config.db);
});
var models = glob.sync(config.root + '/app/models/*.js');
models.forEach(function (model) {
require(model);
});
module.exports = require('./config/express')(app, config);
var gracefulExit = function() {
db.close(function () {""
process.exit(0);
});
}
// If the Node process ends, close the Mongoose connection
process.on('SIGINT', gracefulExit).on('SIGTERM', gracefulExit);
server.listen(config.port, function () {
console.log('Express server listening on port ' + config.port);
});
Vote = mongoose.model('Vote');
var allClients = [];
io.on('connection', function (socket) {
socket.emit('news', 'welcome here new client !');
allClients.push(socket);
socket.on('disconnect', function(){
allClients.splice(allClients.indexOf(socket), 1);
});
});
var interval = setInterval(function(){
var cutoff = new Date();
cutoff.setMinutes(cutoff.getMinutes() - 10);
var query = Vote.find({date : {$gt : cutoff}}).sort({ date: -1 })
query.then(result => {
var voteData = {
nbBullshit: Math.max(result.filter(v => v.vote == 'bullshit').length + getRandomInt(-1,2),0),
nbMeGusta: Math.max(result.filter(v => v.vote == 'meGusta').length + getRandomInt(-1,2),0)
};
allClients.forEach(socket => {
socket.emit('data', voteData);
});
}, error => {
console.error(error);
});
}, 500);