From 04b2455aaff11e563aabfe148be31fb5d4beb32f Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Sun, 9 Mar 2014 20:27:44 +0100 Subject: [PATCH] Simple messages --- lib/models.js | 9 +++++++++ lib/server.js | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/models.js b/lib/models.js index ea87818..a29b6e7 100644 --- a/lib/models.js +++ b/lib/models.js @@ -63,5 +63,14 @@ models.NetworkCollection = Backbone.Collection.extend({ this.add(new models.Network({ 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; + } + } } }); diff --git a/lib/server.js b/lib/server.js index 70b1c19..edd1d53 100644 --- a/lib/server.js +++ b/lib/server.js @@ -20,6 +20,12 @@ Server.prototype.listen = function(port) { this.sockets = io.listen(http).sockets; this.sockets.on("connection", function(socket) { init.call(self, socket); + socket.on( + "input", + function(input) { + handleUserInput.call(self, input); + } + ); }); return this; @@ -31,3 +37,16 @@ function init(socket) { 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 + ); + } +}