From 44fe080270ab84c0bd830f4feba218461e669b9b Mon Sep 17 00:00:00 2001 From: Matthieu Lalonde Date: Thu, 12 Aug 2010 14:57:44 +0200 Subject: [PATCH] pubsub message --- lib/xmpp-client/client.js | 2 ++ lib/xmpp-client/pubsub.js | 13 ++++++++++++- test/test.js | 18 +++++++++++------- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/lib/xmpp-client/client.js b/lib/xmpp-client/client.js index 8f81f44..2dbc3e6 100644 --- a/lib/xmpp-client/client.js +++ b/lib/xmpp-client/client.js @@ -115,7 +115,9 @@ var Client = function(params, callback) { } else { jabber._debug('MESSAGE: ' + stanza); var event_ = stanza.getChild('event', 'http://jabber.org/protocol/pubsub#event'); + sys.debug(event_.toString().green); if(event_ != null) { + sys.debug('beuha'.green); jabber.emit('pubsub:event', from, event_.getChild('items').attrs.node, event_, stanza); } else { jabber.emit('message', from, stanza.getChild('body').getText(), stanza); diff --git a/lib/xmpp-client/pubsub.js b/lib/xmpp-client/pubsub.js index c3565d1..0f572f8 100644 --- a/lib/xmpp-client/pubsub.js +++ b/lib/xmpp-client/pubsub.js @@ -9,6 +9,17 @@ function Pubsub(client, to) { to = 'pubsub.' + this.client.jid.domain; } this.to = to; + this._eventCallback = {}; + var pubsub = this; + this.client.addListener('pubsub:event', function(from, node, event_, stanza) { + if(from == pubsub.to) { + sys.debug('a pubsub event'.yellow); + var cb = pubsub._eventCallback[node]; + if(cb != null) { + cb.call(pubsub, event_, stanza); + } + } + }); }; sys.inherits(Pubsub, events.EventEmitter); @@ -153,7 +164,7 @@ Pubsub.prototype.suscribe = function(node, onMessage, onSuscribed) { .tree(), function(iq) { sys.debug(('Suscribe to ' + node).yellow); - jabber._pubSubCallback[pubsub.to + '#' + node] = onMessage; + pubsub._eventCallback[node] = onMessage; var s = iq.getChild('pubsub', 'http://jabber.org/protocol/pubsub').attrs; onSuscribed.call(jabber, s.subscription, s.subid); } diff --git a/test/test.js b/test/test.js index 7597db9..405ba41 100644 --- a/test/test.js +++ b/test/test.js @@ -96,13 +96,17 @@ exports.testPubSub = function(test) { });*/ p.node(POEMS, function() { sys.debug('got my node'.yellow); - p.suscribe(POEMS, function(item) { - sys.debug('SUSCRIBE : ' + item.toString().yellow); - test.done(); - }, - function(subsription, id) { - p.publish(POEMS, new xmpp.Element('entry', {xmlns: 'http://www.w3.org/2005/Atom'}).c('title').t('blab blah').tree()); - }); + p.suscribe(POEMS, + function(item) { + sys.debug('MESSAGE PUBSUB : ' + item.toString().yellow); + test.done(); + }, + function(subsription, id) { + p.publish(POEMS, new xmpp.Element('entry', {xmlns: 'http://www.w3.org/2005/Atom'}) + .c('title').t('blab blah') + .tree()); + } + ); //test.done(); }); /*