1
0
Fork 0
master
Matthieu Lalonde 14 years ago committed by Mathieu Lecarme
parent 112a8e6302
commit 8ce3836be9

@ -1,2 +1,3 @@
exports.BasicClient = require('./xmpp-client/basic-client').BasicClient;
exports.Client = require('./xmpp-client/client').Client; exports.Client = require('./xmpp-client/client').Client;
exports.Jid = require('./xmpp-client/client').Jid; exports.Jid = require('./xmpp-client/client').Jid;

@ -11,6 +11,8 @@ var sys = require('sys'),
var BasicClient = function(params, callback) { var BasicClient = function(params, callback) {
events.EventEmitter.call(this); events.EventEmitter.call(this);
this._iq = 0; this._iq = 0;
this._iqCallback = {};
this._iqHandler = {};
var jabber = this; var jabber = this;
this.jid = new xmpp.JID(params.jid); this.jid = new xmpp.JID(params.jid);
this.host = (params.host == null) ? this.jid.domain : params.host; this.host = (params.host == null) ? this.jid.domain : params.host;
@ -38,6 +40,12 @@ var BasicClient = function(params, callback) {
break; break;
default: default:
jabber.emit('iq', stanza); jabber.emit('iq', stanza);
var q = stanza.getChild('query');
if(q.attrs.xmlns != null && jabber._iqHandler[q.attrs.xmlns] != null) {
jabber._iqHandler[q.attrs.xmlns].call(jabber, stanza);
} else {
jabber.emit('iq:unknow', stanza);
}
break; break;
} }
break; break;
@ -53,7 +61,6 @@ var BasicClient = function(params, callback) {
break; break;
} }
}); });
this._iqCallback = {};
this.xmpp.addListener('online', function() { this.xmpp.addListener('online', function() {
callback.apply(jabber); callback.apply(jabber);
}); });
@ -87,3 +94,7 @@ BasicClient.prototype.iq = function(to, query, callback, error) {
this.xmpp.send(new xmpp.Element('iq', attrs).cnode(query).tree()); this.xmpp.send(new xmpp.Element('iq', attrs).cnode(query).tree());
return n; return n;
}; };
BasicClient.prototype.registerIqHandler = function(xmlns, action) {
this._iqHandler[xmlns] = action;
};

@ -8,7 +8,6 @@ var sys = require('sys'),
exports.testInit = function(test) { exports.testInit = function(test) {
test.expect(1); test.expect(1);
var b = new BasicClient(conf.b, function() { var b = new BasicClient(conf.b, function() {
sys.debug('just connected');
test.ok(true, 'connected'); test.ok(true, 'connected');
test.done(); test.done();
}); });
@ -19,9 +18,29 @@ exports.testIq = function(test) {
new BasicClient(conf.b, function() { new BasicClient(conf.b, function() {
this.iq(null, new xmpp.Element('query', {xmlns: 'jabber:iq:roster'}), function(iq) { this.iq(null, new xmpp.Element('query', {xmlns: 'jabber:iq:roster'}), function(iq) {
var roster = iq.getChild('query', 'jabber:iq:roster').getChildren('item'); var roster = iq.getChild('query', 'jabber:iq:roster').getChildren('item');
sys.debug(roster); //sys.debug(roster);
test.notEqual(null, roster, 'roster'); test.notEqual(null, roster, 'roster');
test.done(); test.done();
}); });
}); });
};
exports.testPresence = function(test) {
test.expect(1);
var b = new BasicClient(conf.b, function() {
this.addListener('presence', function(presence) {
//sys.debug(presence.attrs.from.split('/')[0].red);
if(presence.attrs.from.split('/')[0] == conf.a.jid) {
test.ok(true, "B is present");
test.done();
}
});
var a = new BasicClient(conf.a, function() {
this.addListener('presence', function(presence) {
//sys.debug(presence.toString().yellow);
});
this.presence();
b.presence();
});
});
}; };
Loading…
Cancel
Save