1
0
Fork 0

node disco and subscribe

master
Matthieu Lalonde 14 years ago committed by Mathieu Lecarme
parent 5da9c8e9fe
commit b3f974ec94

@ -142,7 +142,7 @@ var Client = function(params, callback) {
jabber._iqCallback[id].call(jabber, stanza); jabber._iqCallback[id].call(jabber, stanza);
}); });
this.addListener('pubsub:event', function(from, node, event_, stanza) { this.addListener('pubsub:event', function(from, node, event_, stanza) {
event_.getChildren('item').each(function(item) { event_.getChildren('item').forEach(function(item) {
jabber._pubSubCallback[from + '#' + node].call(jabber, item); jabber._pubSubCallback[from + '#' + node].call(jabber, item);
}); });
}); });

@ -31,7 +31,67 @@ Pubsub.prototype.createTree = function() {
); );
}; };
Pubsub.prototype.discoverNodes = function(callback) { Pubsub.prototype.disco = function(callback) {
var jabber = this.client;
this.client.iq(this.to,
new xmpp.Element('query', {xmlns: 'http://jabber.org/protocol/disco#info'}),
function(iq) {
callback.call(jabber, iq.getChild('query', 'http://jabber.org/protocol/disco#info'));
}
);
};
Pubsub.prototype.discoNode = function(node, callback) {
var jabber = this.client;
this.client.iq(this.to,
new xmpp.Element('query', {xmlns: 'http://jabber.org/protocol/disco#info', node: node}),
function(iq) {
callback.call(jabber, iq.getChild('query', 'http://jabber.org/protocol/disco#info'));
}
);
};
Pubsub.prototype.subscriptions = function(callback) {
var jabber = this.client;
this.client.iq(this.to,
new xmpp.Element('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}).c('subscriptions'),
function(iq) {
callback.call(jabber, iq
.getChild('pubsub', 'http://jabber.org/protocol/pubsub')
.getChild('subscriptions')
.getChildren('subscription')
);
}
);
};
Pubsub.prototype.nodeSubscriptions = function(node, callback) {
var jabber = this.client;
this.client.iq(this.to,
new xmpp.Element('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}).c('subscriptions', {node: node}),
function(iq) {
callback.call(jabber, iq
.getChild('pubsub', 'http://jabber.org/protocol/pubsub')
.getChild('subscriptions')
.getChildren('subscription')
);
}
);
};
Pubsub.prototype.discoNodeItems = function(node, callback) {
var jabber = this.client;
this.client.iq(this.to,
new xmpp.Element('query', {xmlns: 'http://jabber.org/protocol/disco#items', node: node}),
function(iq) {
callback.call(jabber, iq.getChild('query', 'http://jabber.org/protocol/disco#items').getChildren('item'));
}
);
};
Pubsub.prototype.discoNodes = function(callback) {
var jabber = this.client; var jabber = this.client;
this.client.iq(this.to, this.client.iq(this.to,
new xmpp.Element('query', {xmlns: 'http://jabber.org/protocol/disco#items'}), new xmpp.Element('query', {xmlns: 'http://jabber.org/protocol/disco#items'}),
@ -44,7 +104,7 @@ Pubsub.prototype.discoverNodes = function(callback) {
Pubsub.prototype.node = function(node, callback) { Pubsub.prototype.node = function(node, callback) {
var exist = false; var exist = false;
var pubsub = this; var pubsub = this;
this.discoverNodes(function(items) { this.discoNodes(function(items) {
items.forEach(function(item) { items.forEach(function(item) {
if(item.attrs.node == node) { exist = true; } if(item.attrs.node == node) { exist = true; }
}); });
@ -82,17 +142,20 @@ Pubsub.prototype.publish = function(node, content) {
); );
}; };
Pubsub.prototype.suscribe = function(node, callback) { Pubsub.prototype.suscribe = function(node, onMessage, onSuscribed) {
var jabber = this.client; var jabber = this.client;
var pubsub = this;
jabber.iqSet(this.to, new xmpp.Element('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) jabber.iqSet(this.to, new xmpp.Element('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'})
.c('subscription', { .c('subscribe', {
node: node, node: node,
jid: jabber.jid.user + '@' + jabber.jid.domain jid: jabber.jid.user + '@' + jabber.jid.domain
}) })
.tree(), .tree(),
function(stanza) { function(iq) {
sys.debug(stanza.toString().yellow); sys.debug(('Suscribe to ' + node).yellow);
jabber._pubSubCallback[to + '#' + node] = callback; jabber._pubSubCallback[pubsub.to + '#' + node] = onMessage;
var s = iq.getChild('pubsub', 'http://jabber.org/protocol/pubsub').attrs;
onSuscribed.call(jabber, s.subscription, s.subid);
} }
); );
}; };

@ -91,12 +91,18 @@ exports.testPubSub = function(test) {
test.done(); test.done();
}); });
var p = b.pubsub(); var p = b.pubsub();
/*p.discoNode(POEMS, function(r) {
sys.debug(r);
});*/
p.node(POEMS, function() { p.node(POEMS, function() {
sys.debug('got my node'.yellow); sys.debug('got my node'.yellow);
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) {
/*p.suscribe(POEMS, function(item) {
sys.debug('SUSCRIBE : ' + item.toString().yellow); 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());
});
//test.done(); //test.done();
}); });
/* /*

Loading…
Cancel
Save