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