-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.js
96 lines (79 loc) · 2.5 KB
/
chat.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
var _u = require('underscore'),
Backbone = require('backbone'),
MessageCollection = Backbone.Collection.extend({
initialize: function() {
_u.bindAll(this, 'addMessages');
this.bufferLength = 10;
},
comparator: function(message) {
return message.get('time') || 0;
},
addMessages: function(newMessages) {
var bufLength = this.bufferLength,
numMessages = this.length,
modelsToRemove, i, len;
this.add(newMessages);
if (numMessages > bufLength) {
modelsToRemove = this.models.slice(0, numMessages-bufLength);
this.remove(modelsToRemove);
}
},
}),
ChatController = function () {
var _this = this,
io;
this.create = function (data, socket) {
};
this.read = function () {
};
this.update = function (data, socket) {
this.collection.add(data.model);
this.trigger('newMessage', { 'message': data.model,
'callback': this.handleCorrectGuess,
'socket': socket });
socket.json.broadcast.emit('incomingMessage', data.model);
};
this.del = function () {
};
// Function: listen
// ================
// Takes a Socket.IO object and listens on the 'chat'
// namespace.
this.listen = function (socketio) {
io = socketio.of('/chat'); // SocketNamespace
io.on('connection', (function(socket) {
socket.on('newMessages', function(newMessages) {
if (!newMessages || !newMessages.length) {
console.log('[err] empty newMessages in chat');
}
else {
_this.collection.addMessages(newMessages);
_this.trigger('newMessages', { 'messages': newMessages,
'callback': _this.handleCorrectGuess,
'socket': socket });
socket.broadcast.emit('incomingMessages', newMessages);
}
});
socket.on('sync', (function (data) {
this[data.method](data, socket);
}).bind(this));
}).bind(this));
};
this.handleCorrectGuess = function(correctGuess, socket) {
io.emit('notifyCorrectGuess', correctGuess);
};
this.broadcastNewPlayer = function (playerInfo) {
io.emit('newPlayer', playerInfo);
};
this.log = function(str) {
console.log('[CHAT] ' + str);
};
this.initialize = function () {
_u.extend(this, Backbone.Events);
_u.bindAll(this);
this.collection = new MessageCollection();
return this;
};
return this.initialize();
};
module.exports = new ChatController();