Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

start toying with reconnect control flow

still very buggy
  • Loading branch information...
commit 28cdca06b5ec8f4da2521ce4c1bcd9e6bd8c3a2d 1 parent e16a1b2
@astro astro authored
View
1  lib/xmpp/client.js
@@ -39,6 +39,7 @@ function Client(params) {
this.api_key = params.api_key;
this.secret_key = params.secret_key;
this.session_key = params.session_key;
+ this.reconnect = params.reconnect;
this.xmlns[''] = NS_CLIENT;
this.xmppVersion = "1.0";
this.streamTo = this.jid.domain;
View
48 lib/xmpp/component.js
@@ -13,9 +13,10 @@ var NS_COMPONENT = 'jabber:component:accept';
* password: String (required)
* host: String (required)
* port: Number (required)
+ * reconnect: Boolean (optional)
*/
function Component(params) {
- var self = this;
+ var that = this;
Connection.Connection.call(this);
if (typeof params.jid == 'string')
@@ -23,28 +24,53 @@ function Component(params) {
else
this.jid = params.jid;
this.password = params.password;
+ this.host = params.host;
+ this.port = params.port;
+ this.reconnect = params.reconnect;
this.xmlns[''] = NS_COMPONENT;
this.streamTo = this.jid.domain;
this.addListener('streamStart', function(streamAttrs) {
- self.onStreamStart(streamAttrs);
+ that.onStreamStart(streamAttrs);
});
this.addListener('rawStanza', function(stanza) {
- self.onRawStanza(stanza);
- });
- var attempt = SRV.connect(this.socket, [], params.host, params.port);
- attempt.addListener('connect', function() {
- self.startParser();
- self.startStream();
- });
- attempt.addListener('error', function(e) {
- self.emit('error', e);
+ that.onRawStanza(stanza);
});
+
+ this.connect();
+ /*if (this.reconnect)
+ this.socket.addListener('close', function() {
+ console.log('closing')
+ setTimeout(function() {
+ that.connect();
+ }, Math.ceil(Math.random() * 1000));
+ });*/
}
sys.inherits(Component, Connection.Connection);
exports.Component = Component;
+Component.prototype.connect = function() {
+ var that = this;
+ /* Components actually don't use SRV records. We still use this
+ * layer as it provides address family retrying etc.
+ */
+ var attempt = SRV.connect(this.socket, [], this.host, this.port);
+ attempt.addListener('connect', function() {
+ that.startParser();
+ that.startStream();
+ });
+ attempt.addListener('error', function(e) {
+ if (that.reconnect) {
+ console.error('Component: ' + e.message);
+ setTimeout(function() {
+ that.connect();
+ }, Math.ceil(Math.random() * 5000));
+ } else
+ that.emit('error', e);
+ });
+};
+
Component.prototype.onStreamStart = function(streamAttrs) {
var digest = sha1_hex(streamAttrs.id + this.password);
this.send(new ltx.Element('handshake').t(digest));
View
1  lib/xmpp/connection.js
@@ -201,6 +201,7 @@ Connection.prototype.rmStreamNs = function(stanza) {
Connection.prototype.onEnd = function() {
this.stopParser();
this.socket.end();
+ this.emit('end');
};
/**
Please sign in to comment.
Something went wrong with that request. Please try again.