Permalink
Browse files

adding support for the ping XEP

  • Loading branch information...
1 parent d008d54 commit 1f4eadf4deaa80df43a14f4d86e9f63e2751125a @julien51 julien51 committed Mar 16, 2012
Showing with 77 additions and 0 deletions.
  1. +3 −0 lib/server.js
  2. +20 −0 modules/ping.js
  3. +54 −0 test/functional/ping.js
View
@@ -11,6 +11,7 @@ var DiscoInfo = require('../modules/disco_info');
var VCard = require('../modules/vcard');
var Websocket = require('../modules/websocket');
var S2S = require('../modules/s2s');
+var Ping = require('../modules/ping');
// Loading non -xmpp libraries
var User = require('../lib/users.js').User;
@@ -31,6 +32,8 @@ exports.run = function(config, ready) {
VCard.configure(server, config.vcard);
Websocket.configure(server, config.websocket);
S2S.configure(server, config);
+ Ping.configure(server, config.ping);
+
// On Connect event. When a client connects.
server.on("connect", function(client) {
View
@@ -0,0 +1,20 @@
+var xmpp = require('node-xmpp');
+var ltx = require('ltx');
+
+// XEP-0199: XMPP Ping
+// http://xmpp.org/extensions/xep-0199.html
+
+function Ping() {
+}
+
+exports.configure = function(server, config) {
+ server.on('connect', function(client) {
+ client.on('stanza', function(stz) {
+ if (stz.is('iq') && stz.attrs.type === "get" && (stz.getChild('ping', "urn:xmpp:ping"))) {
+ var pong = new ltx.Element('iq', {from: stz.attrs.to, to: stz.attrs.from, id: stz.attrs.id, type: 'result'});
+ client.send(pong);
+ }
+ });
+ });
+}
+
View
@@ -0,0 +1,54 @@
+/**
+* This attempts to connect to the XMPP server and tests the roster features
+**/
+var xmpp = require('node-xmpp');
+var server = require('../../lib/server.js');
+
+var _ = require('underscore');
+var User = require('../../lib/users.js').User;
+
+fixtures = [["bernard@localhost", "bErnArD"]]; // Fixtures
+
+
+describe('Connect client', function(){
+ var cl = null;
+ var pingId = "pingId-1"
+
+ before(function(proceed) {
+ server.run({port: 5222, domain: 'localhost'}, function() {
+ proceed();
+ });
+ });
+
+
+ beforeEach(function(proceed){
+ var ready = _.after(fixtures.length, function() {
+ cl = new xmpp.Client({jid: "bernard@localhost", password: "bErnArD"});
+ cl.on('online', function () {
+ proceed();
+ });
+ });
+ _.each(fixtures, function(fixture) {
+ User.register(fixture[0], fixture[1], {
+ force: true,
+ success: ready,
+ error: function(err) {
+ console.log("Couldn't add users.");
+ process.exit(1);
+ }
+ });
+ });
+ });
+
+ it('should be able to ping the server and get a pong', function(done){
+ cl.on('stanza', function(stanza) {
+ if(stanza.name === "iq" && stanza.id === pingId) {
+ done();
+ }
+ });
+ cl.send(new xmpp.Iq({type: 'get', id: pingId}).c('ping', {xmlns: 'urn:xmpp:ping'}));
+ });
+
+});
+
+

0 comments on commit 1f4eadf

Please sign in to comment.