-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDontPushTheButton.js
More file actions
87 lines (74 loc) · 2.45 KB
/
DontPushTheButton.js
File metadata and controls
87 lines (74 loc) · 2.45 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
Clicks = new Meteor.Collection("Clicks");
Users = new Meteor.Collection("Users");
var connectedUsers = 0;
var _id = '1';
Clicks.allow({
update: function(userId,clicksCollection,field,modifier){
return modifier &&
modifier['$inc'] &&
modifier['$inc'].click &&
modifier['$inc'].click === 1;
}
});
if (Meteor.isClient) {
Template.clicker.clicks = function () {
var v = Clicks.find({_id: _id}).fetch()[0];
return v === undefined ? 0 : v.click;
};
Template.clicker.events({
'click img' : function () {
Clicks.update(_id,{$inc: {click: 1} });
}
});
Template.clicker.rendered = function() {
var clicks = Template.clicker.clicks();
window.document.title = '(' + clicks + ') Dont Push The Button';
};
Template.clicker.count = function() {
if(Users.find() != undefined) {
var userCount = Users.find().fetch().length;
if (userCount < connectedUsers){
// just
toastr.warning(' ', 'A User has disconnected');
}
else if( userCount > connectedUsers){
toastr.info(' ', 'A User has connected');
}
connectedUsers = userCount;
return connectedUsers;
}
return '...';
}
}
if (Meteor.isServer) {
Meteor.startup(function () {
if ( Clicks.find().count()==0){
Clicks.insert({_id: _id, click: 0});
}
Users.remove({});
Meteor.default_server.stream_server.register( Meteor.bindEnvironment( function(socket) {
var intervalID = Meteor.setInterval(function() {
if (socket.meteor_session) {
var connection = {
connectionID: socket.meteor_session.id,
connectionAddress: socket.address,
userID: socket.meteor_session.userId
};
connection.log(socket.address);
socket.id = socket.meteor_session.id;
Users.insert(connection);
Meteor.clearInterval(intervalID);
}
}, 1000);
socket.on('close', Meteor.bindEnvironment(function () {
Users.remove({
connectionID: socket.id
});
}, function(e) {
Meteor._debug("Exception from connection close callback:", e);
}));
}, function(e) {
Meteor._debug("Exception from connection registration callback:", e);
}));
});
}