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);
});
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);
});
});

@ -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;
this.client.iq(this.to,
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) {
var exist = false;
var pubsub = this;
this.discoverNodes(function(items) {
this.discoNodes(function(items) {
items.forEach(function(item) {
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 pubsub = this;
jabber.iqSet(this.to, new xmpp.Element('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'})
.c('subscription', {
.c('subscribe', {
node: node,
jid: jabber.jid.user + '@' + jabber.jid.domain
})
.tree(),
function(stanza) {
sys.debug(stanza.toString().yellow);
jabber._pubSubCallback[to + '#' + node] = callback;
function(iq) {
sys.debug(('Suscribe to ' + node).yellow);
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();
});
var p = b.pubsub();
/*p.discoNode(POEMS, function(r) {
sys.debug(r);
});*/
p.node(POEMS, function() {
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);
});*/
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();
});
/*

Loading…
Cancel
Save