Commit 5551ff01 authored by JC Brand's avatar JC Brand

When prebinding, attach automatically. Fixes #41

This requires that valid jid, rid, sid and bosh_service_url values be given (or
a valid connection object that was already attached).

Updated the docs to reflect these changes.
parent 4253c9ae
......@@ -47,7 +47,7 @@
this.animate = true;
this.auto_list_rooms = false;
this.auto_subscribe = false;
this.bosh_service_url = ''; // The BOSH connection manager URL. Required if you are not prebinding.
this.bosh_service_url = ''; // The BOSH connection manager URL.
this.debug = false;
this.hide_muc_server = false;
this.i18n = locales.en;
......@@ -71,12 +71,49 @@
return text.replace(re, '<a target="_blank" href="$1">$1</a>');
};
this.giveFeedback = function (message, klass) {
$('.conn-feedback').text(message);
$('.conn-feedback').attr('class', 'conn-feedback');
if (klass) {
$('.conn-feedback').addClass(klass);
}
};
this.log = function (txt) {
if (this.debug) {
console.log(txt);
}
};
this.onConnect = function (status) {
if (status === Strophe.Status.CONNECTED) {
converse.log('Connected');
converse.onConnected();
} else if (status === Strophe.Status.DISCONNECTED) {
if ($button) { $button.show().siblings('span').remove(); }
converse.giveFeedback(__('Disconnected'), 'error');
converse.connection.connect(connection.jid, connection.pass, converse.onConnect);
} else if (status === Strophe.Status.Error) {
if ($button) { $button.show().siblings('span').remove(); }
converse.giveFeedback(__('Error'), 'error');
} else if (status === Strophe.Status.CONNECTING) {
converse.giveFeedback(__('Connecting'));
} else if (status === Strophe.Status.CONNFAIL) {
if ($button) { $button.show().siblings('span').remove(); }
converse.giveFeedback(__('Connection Failed'), 'error');
} else if (status === Strophe.Status.AUTHENTICATING) {
converse.giveFeedback(__('Authenticating'));
} else if (status === Strophe.Status.AUTHFAIL) {
if ($button) { $button.show().siblings('span').remove(); }
converse.giveFeedback(__('Authentication Failed'), 'error');
} else if (status === Strophe.Status.DISCONNECTING) {
converse.giveFeedback(__('Disconnecting'), 'error');
} else if (status === Strophe.Status.ATTACHED) {
converse.log('Attached');
converse.onConnected();
}
};
this.toISOString = function (date) {
var pad;
if (typeof date.toISOString !== 'undefined') {
......@@ -2062,7 +2099,7 @@
});
}, this),
$.proxy(function (jid, fullname, img, img_type, url) {
console.log("Error while retrieving vcard");
converse.log("Error while retrieving vcard");
this.add({jid: bare_jid, subscription: 'none', ask: 'request', fullname: jid, is_last: true});
}, this)
);
......@@ -2486,33 +2523,7 @@
$button = $form.find('input[type=submit]');
$button.hide().after('<span class="spinner login-submit"/>');
}
connection.connect(jid, password, $.proxy(function (status, message) {
if (status === Strophe.Status.CONNECTED) {
console.log(__('Connected'));
converse.onConnected(connection);
} else if (status === Strophe.Status.DISCONNECTED) {
if ($button) { $button.show().siblings('span').remove(); }
converse.giveFeedback(__('Disconnected'), 'error');
this.connect(null, connection.jid, connection.pass);
} else if (status === Strophe.Status.Error) {
if ($button) { $button.show().siblings('span').remove(); }
converse.giveFeedback(__('Error'), 'error');
} else if (status === Strophe.Status.CONNECTING) {
converse.giveFeedback(__('Connecting'));
} else if (status === Strophe.Status.CONNFAIL) {
if ($button) { $button.show().siblings('span').remove(); }
converse.giveFeedback(__('Connection Failed'), 'error');
} else if (status === Strophe.Status.AUTHENTICATING) {
converse.giveFeedback(__('Authenticating'));
} else if (status === Strophe.Status.AUTHFAIL) {
if ($button) { $button.show().siblings('span').remove(); }
converse.giveFeedback(__('Authentication Failed'), 'error');
} else if (status === Strophe.Status.DISCONNECTING) {
converse.giveFeedback(__('Disconnecting'), 'error');
} else if (status === Strophe.Status.ATTACHED) {
console.log(__('Attached'));
}
}, this));
connection.connect(jid, password, converse.onConnect);
},
initialize: function (cfg) {
......@@ -2591,14 +2602,6 @@
}
};
this.giveFeedback = function (message, klass) {
$('.conn-feedback').text(message);
$('.conn-feedback').attr('class', 'conn-feedback');
if (klass) {
$('.conn-feedback').addClass(klass);
}
};
this.initStatus = function (callback) {
this.xmppstatus = new this.XMPPStatus();
var id = hex_sha1('converse.xmppstatus-'+this.bare_jid);
......@@ -2618,8 +2621,7 @@
this.rosterview = new this.RosterView({'model':this.roster});
}
this.onConnected = function (connection, callback) {
this.connection = connection;
this.onConnected = function (callback) {
if (this.debug) {
this.connection.xmlInput = function (body) { console.log(body); };
this.connection.xmlOutput = function (body) { console.log(body); };
......@@ -2676,14 +2678,22 @@
if (this.show_controlbox_by_default) {
this.toggleControlBox();
}
if (this.prebind) {
if (!this.connection) {
if ((!this.jid) || (!this.sid) || (!this.rid) || (!this.bosh_service_url)) {
this.log('If you set prebind=true, you MUST supply JID, RID and SID values');
return;
}
this.connection = new Strophe.Connection(this.bosh_service_url);
this.connection.attach(this.jid, this.sid, this.rid, this.onConnect);
} else {
this.onConnected();
}
}
};
return {
'initialize': function (settings) {
converse.initialize(settings);
},
'onConnected': function (connection, callback) {
// onConnected can only be called after initialize has been called.
converse.onConnected(connection, callback);
}
};
}));
......@@ -188,15 +188,17 @@ Jack Moffitt has a great `blogpost`_ about this and even provides an `example Dj
.. Note::
If you want to enable single session support, make sure to pass **prebind: true**
when you call **converse.initialize** (see ./index.html).
Additionally you need to pass in valid **jid**, **sid**, **rid** and
**bosh_service_url** values.
When you authenticate to the XMPP server on your backend, you'll receive two
tokens, RID (request ID) and SID (session ID).
These tokens then need to be passed back to the javascript running in your
browser, where you will need them attach to the existing session.
browser, where you will need them to attach to the existing session.
You can embed the RID and SID tokens in your HTML markup or you can do an
XMLHttpRequest call to you server and ask it to return them for you.
XMLHttpRequest call to your server and ask it to return them for you.
Below is one example of how this could work. An Ajax call is made to the
relative URL **/prebind** and it expects to receive JSON data back.
......@@ -204,26 +206,19 @@ relative URL **/prebind** and it expects to receive JSON data back.
::
$.getJSON('/prebind', function (data) {
var connection = new Strophe.Connection(converse.bosh_service_url);
connection.attach(data.jid, data.sid, data.rid, function (status) {
if ((status === Strophe.Status.ATTACHED) || (status === Strophe.Status.CONNECTED)) {
converse.onConnected(connection)
}
});
}
converse.initialize({
prebind: true,
bosh_service_url: data.bosh_service_url,
jid: data.jid,
sid: data.sid,
rid: data.rid
});
);
**Here's what's happening:**
The JSON data contains the user's JID (jabber ID), RID and SID. The URL to the
BOSH connection manager is already set as a configuration setting on the
*converse* object (see ./main.js), so we can reuse it from there.
A new Strophe.Connection object is instantiated and then *attach* is called with
the user's JID, the necessary tokens and a callback function.
In the callback function, you call *converse.onConnected* together with the
connection object.
The JSON data contains the user's JID (jabber ID), RID, SID and the URL to the
BOSH connection manager.
Facebook integration
......@@ -477,7 +472,7 @@ logged in user.
hide_muc_server
---------------
Default = False
Default = false
Hide the ``server`` input field of the form inside the ``Room`` panel of the
controlbox. Useful if you want to restrict users to a specific XMPP server of
......@@ -486,7 +481,7 @@ your choosing.
prebind
--------
Default = False
Default = false
Use this option when you want to attach to an existing XMPP connection that was
already authenticated (usually on the backend before page load).
......@@ -494,26 +489,19 @@ already authenticated (usually on the backend before page load).
This is useful when you don't want to render the login form on the chat control
box with each page load.
When set to true, you'll need to make sure that the onConnected method is
called, and passed to it a Strophe connection object.
For prebinding to work, your backend server must authenticate for you, and
then return a JID (jabber ID), SID (session ID) and RID (Request ID).
Besides requiring the back-end to authenticate you, you'll also
have to write a Javascript snippet to attach to the set up connection::
If you set ``prebind`` to ``true``, you have to make sure to also pass in these
values as ``jid``, ``sid``, ``rid``.
$.JSON({
'url': 'mysite.com/xmpp-authenticate',
'success': function (data) {
connection = new Strophe.Connection(bosh_service_url);
connection.attach(data.jid, data.sid, data.rid, converse.onConnected);
}
Additionally, you have to specify ``bosh_service_url``.
The backend must authenticate for you, and then return a SID (session ID) and
RID (Request ID), which you use when you attach to the connection.
show_controlbox_by_default
--------------------------
Default = False
Default = false
The "controlbox" refers to the special chatbox containing your contacts roster,
status widget, chatrooms and other controls.
......
......@@ -71,7 +71,6 @@ require([
animate: false
});
converse.onConnected(
mock_connection,
function (converse) {
window.converse = converse;
require([
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment