From 739fbc74536b913817b987bd18874d34030f5cd1 Mon Sep 17 00:00:00 2001 From: Matthieu Lalonde Date: Mon, 9 Aug 2010 21:59:49 +0200 Subject: [PATCH] rooms without a blunderer --- lib/xmpp-client/client.js | 14 +++++++++++--- test/test.js | 26 +++++++++++++------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/lib/xmpp-client/client.js b/lib/xmpp-client/client.js index a9d4513..89921ae 100644 --- a/lib/xmpp-client/client.js +++ b/lib/xmpp-client/client.js @@ -11,6 +11,7 @@ var Client = function(params, callback) { this.jid = new xmpp.JID(params.jid); this.host = (params.host == null) ? this.jid.domain : params.host; this.rooms = {}; + this._isReady = false; this._iq = 0; this._iqHandler = {}; this._iqCallback = {}; @@ -86,7 +87,7 @@ var Client = function(params, callback) { }); this.addListener('groupchat', function(from, msg, stanza) { fromName = from.split('/')[0]; - jabber.rooms[fromName].emit('message', msg, stanza); + jabber.rooms[fromName].emit('message', from, msg, stanza); }); this.addListener('iqResult', function(id, stanza){ jabber._iqCallback[id].call(jabber, stanza); @@ -166,7 +167,10 @@ Client.prototype.askForRoster = function(callback) { subscription: child.attrs.subscription}; }); if(callback != null) { - callback.call(jabber, jabber.roster); + if(! jabber._isReady) { //[FIXME] will not work if askForRoster is called again + jabber._isReady = true; + callback.call(jabber, jabber.roster); + } } jabber.emit('roster', jabber.roster); }); @@ -221,6 +225,7 @@ Client.prototype.disconnect = function() { var Room = function(client, name, callback) { events.EventEmitter.call(this); + this._isReady = false; this.client = client; this.room = name; this.to = this.room + '/' + this.client.jid.user; @@ -236,7 +241,10 @@ var Room = function(client, name, callback) { room.role = item.attrs.role; } var status = x.getChild('status'); - callback.call(room, (status != null) ? status.attrs.code : '200'); + if(! room._isReady) { + room._isReady = true; + callback.call(room, (status != null) ? status.attrs.code : '200'); + } } } }); diff --git a/test/test.js b/test/test.js index c416550..f3c534b 100644 --- a/test/test.js +++ b/test/test.js @@ -48,34 +48,34 @@ exports.testClient = function(test) { */ exports.testRoom = function(test) { - //test.expect(1); + test.expect(1); var ROOM = 'mushroom@conference.' + conf.b.jid.split('@')[1]; var MESSAGE = "Hello everybody"; + var cpt = 0; var b = new Client(conf.b, function() { sys.debug('b is connected'.red); sys.debug(('enter in ' + ROOM).green); //console.log(sys.inspect(this, true, null)); var b_room = b.room(ROOM, function(status) { + sys.debug('b room is created'.green); this.addListener('message', function(from, msg, stanza) { - sys.debug(msg.yellow); - test.done(); + sys.debug(from.yellow); + test.equals(MESSAGE, msg); + if(MESSAGE == msg) { + test.done(); + } }); - this.message(MESSAGE); -/* var a = new Client(conf.a, function() { + var a = new Client(conf.a, function() { sys.debug('a is connected'.green); var a_room = a.room(ROOM, function(status) { - sys.debug(status); - sys.debug(this.role); + sys.debug(status.green); + sys.debug(this.role.green); this.addListener('message', function(from, msg, stanza) { sys.debug('message : ' + msg); - test.equals(MESSAGE, msg); - if(MESSAGE == msg) { - test.done(); - } }); - b_room.message(MESSAGE); + this.message(MESSAGE); }); - });*/ + }); }); }); };