Simple messages

This commit is contained in:
Mattias Erming 2014-03-09 20:27:44 +01:00
parent a783a71789
commit 04b2455aaf
2 changed files with 28 additions and 0 deletions

View File

@ -63,5 +63,14 @@ models.NetworkCollection = Backbone.Collection.extend({
this.add(new models.Network({ this.add(new models.Network({
host: "Lobby" host: "Lobby"
})); }));
},
getChannel: function(id) {
var networks = this.models;
for (var i = 0; i < networks.length; i++) {
var find = networks[i].get("channels").findWhere({id: id});
if (find) {
return find;
}
}
} }
}); });

View File

@ -20,6 +20,12 @@ Server.prototype.listen = function(port) {
this.sockets = io.listen(http).sockets; this.sockets = io.listen(http).sockets;
this.sockets.on("connection", function(socket) { this.sockets.on("connection", function(socket) {
init.call(self, socket); init.call(self, socket);
socket.on(
"input",
function(input) {
handleUserInput.call(self, input);
}
);
}); });
return this; return this;
@ -31,3 +37,16 @@ function init(socket) {
this.networks this.networks
) )
} }
function handleUserInput(input) {
var channel = this.networks.getChannel(input.id);
if (channel) {
channel.get("messages").push(
new models.Message({user: "user", text: input.text})
);
this.sockets.emit(
"event",
this.networks
);
}
}