Created
October 30, 2013 06:31
-
-
Save johnf/7228007 to your computer and use it in GitHub Desktop.
Simple jabber server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Based on code at https://github.com/astro/node-xmpp/blob/master/examples/c2s.js | |
var xmpp = require('node-xmpp'); | |
var c2s = new xmpp.C2SServer({ | |
port: 5222, | |
domain: 'localhost' | |
}); | |
var sessions = {}; | |
c2s.on('connect', function(client) { | |
c2s.on('register', function(opts, cb) { | |
cb(true); | |
}); | |
client.on('authenticate', function(opts, cb) { | |
// Auth everyone | |
cb(null); | |
}); | |
client.on('online', function() { | |
sessions[client.jid.bare().toString()] = client; | |
sessions[client.jid.toString()] = client; | |
}); | |
client.on('stanza', function(stanza) { | |
if (stanza.is('message')) { | |
// This might need to be different for Flapjack as it cares about conferences | |
// Will need to track members and then fan out the message to them | |
if (stanza.attrs.to.match(/conference/)) { return; } | |
sessions[stanza.attrs.to].send(stanza); | |
} | |
else if (stanza.is('presence')) { | |
if (stanza.attrs.type == 'subscribe' || stanza.attrs.type == 'subscribed') { | |
sessions[stanza.attrs.to].send(stanza); | |
} | |
} | |
}); | |
client.on('disconnect', function() { | |
delete sessions[client.jid.bare().toString()]; | |
delete sessions[client.jid.toString()]; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment