-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
132 lines (122 loc) · 3.31 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var express = require('express');
var app = express();
var monk = require('monk');
var http = require('http').Server(app);
var io = require('socket.io')(http);
var events = require('events');
var db = monk("localhost/heart");
var col = db.get("db");
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
app.get('/admin', function (req, res) {
res.sendFile(__dirname + '/admin.html');
});
app.get('/about', function (req, res) {
res.sendFile(__dirname + '/about.html');
});
function estatic(name){
app.use('/'+name,express.static(name));
}
estatic('js');
estatic('css');
estatic("bower_components");
var eventEmitter = new events.EventEmitter();
eventEmitter.setMaxListeners(100);
app.get("/post",function(req,res){
//authentication added here
var bpm = req.query.bpm
if(bpm !== "---"){
var user = req.query.user || "You"
if (user==="You"){
console.log("Got post post request: ");
console.log(req.query);
}
var time = new Date();
var rawdata = {time:time,bpm:bpm,user:user};
var dataPromise = col.find({user:user});
dataPromise.then(function(data){
col.insert(rawdata);
if(data.length === 0){
eventEmitter.emit("new_name",user);
}
eventEmitter.emit(user);
});
res.send(rawdata);
} else {
res.send("");
}
});
app.get("/drop",function(req,res){
db.driver.open(function(_,db2){
if(db2 === null){
//sometimes db is null. If it is, wait and try this function again.
setTimeout(function(){
drop(done);
},1000);
} else {
db2.dropDatabase(function(ignore,status){
if(status){
console.log("Dropped database");
} else {
console.log("Error dropping mongod");
console.log(error);
}
});
}
});
res.redirect("/admin");
});
io.on('connection',function(socket){
function create_name_event(name){
eventEmitter.on(name,function(){
var time = new Date(new Date() - (5000 * 60));
var dataPromise = col.find({user:name,time:{$gt:time}},{sort:{$natural:1}});
dataPromise.then(function(data){
for(var i = 0; i < data.length; i++){
delete data[i]._id;
delete data[i].user;
}
socket.emit(name,data);
});
});
eventEmitter.on("all",function(){
eventEmitter.emit(name);
});
socket.on(name,function(){
eventEmitter.emit(name);
});
}
eventEmitter.on("new_name",function(name){
create_name_event(name);
var dataPromise = col.distinct("user",{});
dataPromise.then(function(data){
data = data.sort();
socket.emit("names",data);
});
});
var dataPromise = col.distinct("user",{});
dataPromise.then(function(data){
data = data.sort();
for(var i = 0; i < data.length; i++){
create_name_event(data[i]);
}
var index = data.indexOf("You");
if(index != -1){
data.splice(index,1);
data = ["You"].concat(data);
}
socket.emit("names",data);
});
socket.on("disconnect",function(){
eventEmitter.removeAllListeners();
});
});
setInterval(function(){
eventEmitter.emit("all");
},1000)
var server = http.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});