-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnowjs.js
69 lines (55 loc) · 1.99 KB
/
nowjs.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
function NowjsApp() {
}
// url handling
NowjsApp.prototype.index = function(req, res) {
console.log('Opening up the nowjs sample');
var loginName='';
if (req.session.oauth) {
loginName = req.session.user.name;
}
res.render('now/index', {layout: 'now/layout', locals:{loginName:loginName}})
};
// nowjs configuration
var now = require('now');
var everyone;
var buffer = [];
NowjsApp.prototype.init = function(app) {
addToLog("Initializing the nowjs app");
everyone = now.initialize(app, {clientWrite: false});
everyone.now.availablePersons = [];
everyone.now.distributeMessage = function(message) {
console.log('Received a message to distribute: %s for %s', message, everyone.now.availablePersons[this.user.clientId]);
var msg = { chat: [everyone.now.availablePersons[this.user.clientId], message] };
buffer.push(msg);
if (buffer.length > 15) buffer.shift();
everyone.now.receiveMessage(msg);
};
everyone.now.setName = function(name) {
addToLog("Set name to: " + name);
everyone.now.availablePersons[this.user.clientId] = name;
everyone.now.newlyJoined(name);
sendClients();
};
everyone.on('connect', function() {
addToLog("Joined: " + this.user.clientId);
this.now.bufferedMessages({buffer:buffer});
});
everyone.on('disconnect', function() {
addToLog("Left: " + this.user.clientId);
delete everyone.now.availablePersons[this.user.clientId];
everyone.now.hasLeft(this.user.clientId);
sendClients();
});
function addToLog(message) {
console.log(message);
}
function sendClients() {
var curClients = [];
for (var i in everyone.now.availablePersons) {
curClients[curClients.length] = everyone.now.availablePersons[i];
}
console.log("Number of clients: " + curClients);
everyone.now.refreshAvailablePersons({users: curClients});
}
};
module.exports = NowjsApp;