From e632451fec8be5d5ec6a39d7789f91e0c7feaeb3 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Fri, 22 Feb 2013 14:14:14 -0800 Subject: we have a chat --- public/js/game.js | 60 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 20 deletions(-) (limited to 'public/js/game.js') diff --git a/public/js/game.js b/public/js/game.js index 7bdabbf..461d96e 100644 --- a/public/js/game.js +++ b/public/js/game.js @@ -1,24 +1,44 @@ var socket = io.connect(window.location.hostname); -var imageCount = 0; -socket.on('join', function(json) { - var data = JSON.parse(json); - for (var i in data) { - appendImage(data[i]); - } -}); -socket.on('url', function(json) { - var data = JSON.parse(json); - appendImage(data); -}); -function appendImage(img) { - var image = new Image(); - image.src = img.url; - $("#images").prepend(image); - imageCount++; - while (imageCount > 20) { - $("#images").children().last().remove(); - imageCount--; - } +var Game = { + nick: "ryz", + + init: function(){ + + // Bind events from the server + socket.on('event-join', Events.receive.join); + socket.on('event-message', Events.receive.message); + Chat.init(); + } } +var Events = { + + receive: { + join: function(data){ + console.log("RECEIVE JOIN") + for (var i in data.messages) { + Chat.add(data.messages[i]); + } + }, + + message: function(msg){ +console.log("RECEIVE MESSAGE") + Chat.add(msg); + } + }, + + send: { + + message: function(msg) { +console.log("SE D MMESSAGe") + socket.emit('event-message', msg); + } + + }, + +}; + + +$(Game.init); + -- cgit v1.2.3-70-g09d2