Skip to content

Commit

Permalink
Event Emitter added
Browse files Browse the repository at this point in the history
  • Loading branch information
arunoda committed Aug 12, 2011
1 parent 8b9ba02 commit cb6ed2a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
1 change: 1 addition & 0 deletions examples/echo.js
Expand Up @@ -43,5 +43,6 @@ xmpp.connect({
jid : argv[2],
password : argv[3],
host : 'talk.google.com',
host : 'talk.google.com',
port : 5222
});
23 changes: 8 additions & 15 deletions lib/simple-xmpp.js
Expand Up @@ -25,27 +25,26 @@
*/

var xmpp = require('node-xmpp');
var EventEmitter = require('events').EventEmitter;

module.exports = new SimpleXMPP();

function SimpleXMPP() {

var conn;

var chatCallback;
var errorCallback;
var onlineCallback;
var events = new EventEmitter();

this.onChat = function(callback) {
chatCallback = callback;
events.on('chat', callback);
};

this.onError = function(callback) {
errorCallback = callback;
events.on('error', callback);
};

this.onOnline = function(callback) {
onlineCallback = callback;
events.on('online', callback);
};

this.send = function(to, message) {
Expand All @@ -68,9 +67,7 @@ function SimpleXMPP() {

conn.on('online', function(){
conn.send(new xmpp.Element('presence'));
if(onlineCallback) {
onlineCallback();
}
events.emit('online');
});

conn.on('stanza', function(stanza) {
Expand All @@ -80,17 +77,13 @@ function SimpleXMPP() {
if(chat.name == 'body') {
var message = stanza.children[0].children[0];
var from = stanza.attrs.from;
if(chatCallback) {
chatCallback(from, message);
}
events.emit('chat', from, message);
}
}
});

conn.on('error', function(err) {
if(errorCallback) {
errorCallback(err);
}
events.emit('error', err);
});

};
Expand Down

0 comments on commit cb6ed2a

Please sign in to comment.