Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 49 additions & 39 deletions lib/reactnative-xmpp.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Client = function (conf) {
+ 'version="1.0" >');
}

function resourceBindings () {
function resourceBindings () {
self.sendIq({type: 'set'}, '<bind xmlns="' + NS_BIND + '"><resource>' + conf.resource + "</resource></bind>", function(elt) {
sessionBindings();
});
Expand All @@ -118,16 +118,17 @@ Client = function (conf) {
function sessionBindings () {
console.log("In session bindings");
self.sendIq({type: 'set'}, '<session xmlns="' + NS_SESSION + '"></session>', function(elt) {
sendPresenceInformation();
});
sendPresenceInformation();
});
}

function sendPresenceInformation () {
console.log("In send presence information");
self.getRoster(function () {
self.setStatus('chat', conf.status);
self.emit('ready');
});
self.emit('ready');
// self.getRoster(function () {
// self.setStatus('chat', conf.status);
//
// });
}

function onMessageReceive (attrs) {
Expand All @@ -140,13 +141,14 @@ Client = function (conf) {
self.emit('html.message', element.parent);
}

function onMessageReceived () {
stream_events_manager.removeListener('end|body|' + NS_XHTML, onHtmlBodyReceived);
stream_events_manager.removeListener('end|body|' + NS_CLIENT, onBodyReceived);
function onMessageReceived (element) {
self.emit('message', element);
// stream_events_manager.removeListener('end|body|' + NS_XHTML, onHtmlBodyReceived);
// stream_events_manager.removeListener('end|body|' + NS_CLIENT, onBodyReceived);
}

stream_events_manager.once('end|body|' + NS_XHTML, onHtmlBodyReceived);
stream_events_manager.once('end|body|' + NS_CLIENT, onBodyReceived);
// stream_events_manager.once('end|body|' + NS_XHTML, onHtmlBodyReceived);
// stream_events_manager.once('end|body|' + NS_CLIENT, onBodyReceived);
stream_events_manager.once('end|message|' + NS_CLIENT, onMessageReceived);
}

Expand All @@ -166,9 +168,9 @@ Client = function (conf) {

function presenceReceived (element) {
if(element.getAttribute('type') !== null && element.getAttribute('type') !== '') {
self.emit('presence.'+ element.getAttribute('type'), element);
self.emit('presence.'+ element.getAttribute('type'), element);
}

self.emit('presence.receive', element);
}

Expand Down Expand Up @@ -224,20 +226,21 @@ Client = function (conf) {
}

function init () {
//
self.jid = conf.login + '@' + conf.domain + '/' + conf.resource;
conn = tcp.createConnection(conf.port || 5222, conf.host);

conn.on("disconnect", function (error) {
if (error) {
throw "disconnected server in error";
}
self.emit('disconnect');
// if (error) {
// throw "disconnected server in error";
// }
});

conn.on("connect", function () {
this.setTimeout(0);
this.setEncoding("utf8");
initiateSession();
self.emit('connecting');
});

conn.on("data", function (data) {
Expand All @@ -256,32 +259,36 @@ Client = function (conf) {
});

conn.on("error", function (error) {
console.error(error);
var timeout = Math.round(Math.random() * 10 + 5) * 1000;
// console.error(error);
self.emit('error');
var timeout = Math.round(5) * 1000;
setTimeout(init, timeout);
console.log(timeout);
// console.log(timeout);
});

conn.on("close", function (error) {
if (error) {
throw 'connection close with error';
}
self.emit('close');
// var timeout = 5 * 1000;
// setTimeout(init, timeout);
// if (error) {
// throw 'connection close with error';
// }
});

conn.on("end", function () {
this.write('</stream:stream>');

});
}


stream_events_manager = (new events)
.once('start|stream|' + NS_STREAMS, setID)
.once('start|success|' + NS_SASL, initiateSession)
.once('start|bind|' + NS_BIND, resourceBindings)
.once('start|presence|' + NS_CLIENT, onPresenceReceive)
.once('end|mechanism|' + NS_CLIENT, setMechanism)
.once('end|mechanisms|' + NS_SASL, sasl)
.on('start|stream|' + NS_STREAMS, setID)
.on('start|success|' + NS_SASL, initiateSession)
.on('start|bind|' + NS_BIND, resourceBindings)
.on('start|presence|' + NS_CLIENT, onPresenceReceive)
.on('end|mechanism|' + NS_CLIENT, setMechanism)
.on('end|mechanisms|' + NS_SASL, sasl)
.on('start|message|' + NS_CLIENT, onMessageReceive)
.on('start|query|' + NS_SOFT_VERSION, sendSoftVersion)
.on('start|query|' + NS_LAST_SRV_RCV, sendLastServerResponse)
Expand Down Expand Up @@ -327,8 +334,8 @@ Client = function (conf) {
value = '';
current_node.cdata = cdata;
cdata = '';
stream_events_manager.emit('end|' + elem + '|' + uri, current_node);
current_node = parent_node = current_node.parent;
stream_events_manager.emit('end|' + elem + '|' + uri, current_node);
current_node = parent_node = current_node.parent;
});
cb.onCharacters(function(chars) {
value += chars;
Expand Down Expand Up @@ -459,11 +466,9 @@ Client.prototype.getVCard = function (to, cb) {
*
* Send instant message.
**/
Client.prototype.push = function (to, str, type, data = undefined) {
if (type === "groupchat") {
to = this.getBareJid(to);
}
Client.prototype.push = function (id, to, str, type, data = undefined) {
var raw = '<message from="' + this.jid + '" '
+ ' id="' + id + '"'
+ (typeof type === "string" ? ' type="' + type + '"' : ' type="chat"')
+ ' to="' + to + '">' + (data ? data : "") + '<body><![CDATA[' + str + ']]></body></message>';
this.write(raw);
Expand Down Expand Up @@ -608,12 +613,12 @@ Client.prototype.subscribe = function (to, group, name, cb) {
*
* Add somebody to your roster
**/
Client.prototype.join = function (room, nick, cb) {
Client.prototype.join = function (room, nick, history, cb) {

if (!(typeof cb === "function")) {
cb = function () {};
}
return this.write('<presence from="'+ this.jid +'" to="'+ room + '/' + nick + '"><x xmlns="http://jabber.org/protocol/muc#user"><item affiliation="member" role="participant" /></x></presence>');
return this.write('<presence from="'+ this.jid +'" to="'+ room + '/' + nick + '"><x xmlns="http://jabber.org/protocol/muc"><history since="'+history+'"/></x><x xmlns="http://jabber.org/protocol/muc#user"><item affiliation="member" role="participant" /></x></presence>');
};

/**
Expand All @@ -628,6 +633,11 @@ Client.prototype.write = function (data) {
return this;
};

Client.prototype.disconnect = function () {
conn.removeAllListeners()
conn.destroy()
};

exports.Client = Client;
exports.NS_SASL = NS_SASL;
exports.NS_BIND = NS_BIND;
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
"querystring-es3": "^0.2.1",
"react": "15.2.1",
"react-native-crypto": "^2.0.1",
"react-native-fs": "^1.5.1",
"react-native-fs": "^2.3.2",
"react-native-http": "github:tradle/react-native-http#834492d",
"react-native-level-fs": "^2.0.1",
"react-native-randombytes": "^2.1.0",
"react-native-tcp": "^1.0.1",
"react-native-tcp": "git+https://github.com/srijan02420/react-native-tcp.git",
"react-native-udp": "^1.2.4",
"react-native-util": "^1.0.2",
"readable-stream": "^1.0.33",
Expand Down