prebind.js
2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// http-pre-bind example
// This example works with mod_http_pre_bind found here:
// http://github.com/thepug/Mod-Http-Pre-Bind
//
// It expects both /xmpp-httpbind to be proxied and /http-pre-bind
//
// If you want to test this out without setting it up, you can use Collecta's
// at http://www.collecta.com/xmpp-httpbind and
// http://www.collecta.com/http-pre-bind
// Use a JID of 'guest.collecta.com' to test.
var BOSH_SERVICE = '/xmpp-httpbind';
var PREBIND_SERVICE = '/http-pre-bind';
var connection = null;
function log(msg)
{
$('#log').append('<div></div>').append(document.createTextNode(msg));
}
function rawInput(data)
{
log('RECV: ' + data);
}
function rawOutput(data)
{
log('SENT: ' + data);
}
function onConnect(status)
{
if (status === Strophe.Status.CONNECTING) {
log('Strophe is connecting.');
} else if (status === Strophe.Status.CONNFAIL) {
log('Strophe failed to connect.');
$('#connect').get(0).value = 'connect';
} else if (status === Strophe.Status.DISCONNECTING) {
log('Strophe is disconnecting.');
} else if (status === Strophe.Status.DISCONNECTED) {
log('Strophe is disconnected.');
$('#connect').get(0).value = 'connect';
} else if (status === Strophe.Status.CONNECTED) {
log('Strophe is connected.');
connection.disconnect();
} else if (status === Strophe.Status.ATTACHED) {
log('Strophe is attached.');
connection.disconnect();
}
}
function normal_connect() {
log('Prebind failed. Connecting normally...');
connection = new Strophe.Connection(BOSH_SERVICE);
connection.rawInput = rawInput;
connection.rawOutput = rawOutput;
connection.connect($('#jid').val(), $('#pass').val(), onConnect);
}
function attach(data) {
log('Prebind succeeded. Attaching...');
connection = new Strophe.Connection(BOSH_SERVICE);
connection.rawInput = rawInput;
connection.rawOutput = rawOutput;
var $body = $(data.documentElement);
connection.attach($body.find('jid').text(),
$body.attr('sid'),
parseInt($body.attr('rid'), 10) + 1,
onConnect);
}
$(document).ready(function () {
$('#connect').bind('click', function () {
var button = $('#connect').get(0);
if (button.value == 'connect') {
button.value = 'disconnect';
// attempt prebind
$.ajax({
type: 'POST',
url: PREBIND_SERVICE,
contentType: 'text/xml',
processData: false,
data: $build('body', {
to: Strophe.getDomainFromJid($('#jid').val()),
rid: '' + Math.floor(Math.random() * 4294967295),
wait: '60',
hold: '1'}).toString(),
dataType: 'xml',
error: normal_connect,
success: attach});
} else {
button.value = 'connect';
if (connection) {
connection.disconnect();
}
}
});
});