From 071f1d445f810e9861d27ef8eb0b852892eff5dc Mon Sep 17 00:00:00 2001 From: Arnout Kazemier Date: Tue, 19 Apr 2011 22:44:42 +0200 Subject: [PATCH] attaching the front-ends event to the back end events.. --- app.js | 15 ++++++ lib/manager.js | 6 +-- static/js/boot.js | 123 ++++++++++++++++++++++++++++++++-------------- 3 files changed, 103 insertions(+), 41 deletions(-) diff --git a/app.js b/app.js index de7c66c..668fc87 100644 --- a/app.js +++ b/app.js @@ -152,6 +152,21 @@ io.on('connection', function( client ){ // announce that a new user has joined io.publish(client,client.rooms,channel.filter({type:"user:join", nickname:client.nickname, avatar:client.avatar, details:client.details }, client)); }); + + // handle messages from the clients + client.on('comment', function(data){ + var clean = channel.filter(data, client) + , words = clean.message.split( /\s+/g ).length; + + client.details.lines = client.details.lines ? client.details.lines + 1 : 1; + client.details.words = client.details.words ? client.details.words + words : words; + client.rooms && io.publish(client, client.rooms, clean); + }); + + // forwards! + client.on('private', function(data){ + channel.private(data,client); + }); }); /* io.on("connection", function(client){ diff --git a/lib/manager.js b/lib/manager.js index eb041ca..aac94fe 100644 --- a/lib/manager.js +++ b/lib/manager.js @@ -202,7 +202,7 @@ var Manager = module.exports = function(io){ api.subscribe(user); // Notify the user - api.update(user); + api.heartbeat(user); } } }, @@ -214,9 +214,9 @@ var Manager = module.exports = function(io){ * @param {Socket.IO.Client} user The user that receives an update * @api public */ - update: function(user){ + heartbeat: function(user){ user.send(api.filter({ - type: "update" + type: "heartbeat" , nickname: user.nickname }, user)); }, diff --git a/static/js/boot.js b/static/js/boot.js index d750ced..72317dd 100644 --- a/static/js/boot.js +++ b/static/js/boot.js @@ -45,6 +45,10 @@ }) , url: '/users/' , join: function(details){ + // create a id attribute if it doesn't exist so we can + // find them this user back + details.id = details.id || details.nickname; + var friend = new this.model(details); this.add(friend); @@ -139,7 +143,7 @@ * @api public */ private: function(to, message){ - io.send({type:'private', to: to, message: message.toString(), nickname: EventedParser.io.nickname}); + EventedParser.io.send({type:'private', to: to, message: message.toString(), nickname: EventedParser.io.nickname}); }, /** @@ -150,7 +154,7 @@ * @api public */ blacklist: function(nickname){ - io.send({type:'blacklist', blacklist:nickname.toString(), nickname: EventedParser.io.nickname}); + EventedParser.io.send({type:'blacklist', blacklist:nickname.toString(), nickname: EventedParser.io.nickname}); }, /** @@ -161,39 +165,42 @@ * @api public */ send: function(message){ - var request = {type:'message', message: message.toString(), nickname: io.nickname, rooms: io.rooms}; - io.send(request); - EventedPaser.emit('channel:message', request) - } - }, - - /** - * Sends a check to the server to validate the field and value, this is needed - * because we will be working with allot of concurrent users and double values may not - * be permitted by the server instance. - * - * @param {String} field The field that needs to be validated - * @param {String} value The value of the field - * - * @api public - */ - check: function(field,value){ - this.io.send({type:'validate:check', field:field, value:value}) - }, - - /** - * Create a new account the server - * - * @param {String} nickname The nickname for the chat - * @param {String} email The email address - * - * @api public - */ - createAccount: function(nickname, email){ - nickname = '' + nickname; - email = '' + email; + // clean the message + message.replace + + var request = {type:'comment', message: message.toString(), nickname: io.nickname, rooms: io.rooms}; + + EventedParser.io.send(request); + }, + + /** + * Sends a check to the server to validate the field and value, this is needed + * because we will be working with allot of concurrent users and double values may not + * be permitted by the server instance. + * + * @param {String} field The field that needs to be validated + * @param {String} value The value of the field + * + * @api public + */ + check: function(field,value){ + EventedParser.io.send({type:'validate:check', field:field, value:value}) + }, - this.io.send({type: 'account:create', nickname: nickname, email:email }); + /** + * Create a new account the server + * + * @param {String} nickname The nickname for the chat + * @param {String} email The email address + * + * @api public + */ + createAccount: function(nickname, email){ + nickname = '' + nickname; + email = '' + email; + + EventedParser.io.send({type: 'account:create', nickname: nickname, email:email }); + } } }; _.extend(EventedParser, Backbone.Events); @@ -278,7 +285,7 @@ } }); - EventedParser.createAccount(nickname, email); + EventedParser.API.createAccount(nickname, email); } }); }, @@ -354,7 +361,7 @@ .live('keyup', function(){ var value = nickname.val(); if (value.length >= 3) - EventedParser.check('nickname', value); + EventedParser.API.check('nickname', value); }) /** @@ -396,7 +403,7 @@ .live('keyup', function(){ var value = email.val(); if (value.length >= 3 ) - EventedParser.check('email', value); + EventedParser.API.check('email', value); }); // we are done, so flag it @@ -409,13 +416,53 @@ setup: function(nickname){ var self = this , me = self.me - , account = me.account; + , account = me.account + , box = $('.box'); this.state = 'loggedin'; + // Add the listeners + EventedParser.on('private', function(){ + // handle private messages + }); + EventedParser.on('announcement', function(){ + // handle announcements + }); + EventedParser.on('heartbeat', function(){ + // handle heartbeats from the server + }); + EventedParser.on('comment', function(){ + + }); + + EventedParser.on('user:join', function(data){ + Outsiders.join({ + nickname: data.nickname + , avatar: data.avatar + , rooms: data.rooms // we share the same rooms + }); + + }); + EventedParser.on('user:depart', function(data){ + Outsiders.remove(data.nickname) + }); + EventedParser.on('user:nickchange', function(){}); + // prepare the application interface $('#masthead .salut a').html(account.nickname).attr('href', '#/details/' + account.nickname); + // attach the event listeners + box.find('form[name="chat"]').live("submit", function(e){ + e && e.preventDefault(); + + var $self = $(this) + , input = $self.find('input[name="message"]'); + + EventedParser.API.send(input.val()); + + input.val(''); // clear the value + }); + // now that all UI changes are made, we can make the application visable, this way we don't // trigger unnessesary reflows + paint events in the browser. $('html').addClass('loggedin').find('.auth').hide().end().find('div.app').show();