[2271] | 1 | var ProtoPing = { |
---|
| 2 | BOSH_SERVICE: '/xmpp-httpbind', |
---|
| 3 | TARGET: 'jabber.org', |
---|
| 4 | connection: null |
---|
| 5 | }; |
---|
| 6 | |
---|
| 7 | document.observe('dom:loaded', function () { |
---|
| 8 | var log = $('log'); |
---|
| 9 | |
---|
| 10 | $('connect').observe('click', function () { |
---|
| 11 | var jid = $F('jid'); |
---|
| 12 | var pass = $F('pass'); |
---|
| 13 | Form.Element.setValue('pass', ''); |
---|
| 14 | |
---|
| 15 | ProtoPing.connection = new Strophe.Connection(ProtoPing.BOSH_SERVICE); |
---|
| 16 | |
---|
| 17 | log.insert("<p>Connecting...</p>"); |
---|
| 18 | |
---|
| 19 | ProtoPing.connection.connect(jid, pass, function (status) { |
---|
| 20 | if (status === Strophe.Status.CONNECTED) { |
---|
| 21 | document.fire('strophe:connected'); |
---|
| 22 | } else if (status === Strophe.Status.DISCONNECTED) { |
---|
| 23 | document.fire('strophe:disconnected'); |
---|
| 24 | } |
---|
| 25 | }); |
---|
| 26 | }); |
---|
| 27 | |
---|
| 28 | document.observe('strophe:connected', function () { |
---|
| 29 | log.insert('<p>Connected.</p>'); |
---|
| 30 | |
---|
| 31 | var ping = $iq({to: ProtoPing.TARGET, type: 'get'}) |
---|
| 32 | .c('query', {xmlns: Strophe.NS.DISCO_ITEMS}); |
---|
| 33 | |
---|
| 34 | var sent_stamp = new Date(); |
---|
| 35 | ProtoPing.connection.sendIQ(ping, function (iq) { |
---|
| 36 | var elapsed = new Date() - sent_stamp; |
---|
| 37 | |
---|
| 38 | // use Prototype's selectors to access XMPP stanza |
---|
| 39 | var items = Selector.findChildElements(iq, ['item']); |
---|
| 40 | |
---|
| 41 | log.insert("<p>Disco#items response received after " + |
---|
| 42 | elapsed + "ms." + ProtoPing.TARGET + " reports " + |
---|
| 43 | "it has " + items + " disco items.</p>"); |
---|
| 44 | |
---|
| 45 | ProtoPing.connection.disconnect(); |
---|
| 46 | }); |
---|
| 47 | |
---|
| 48 | ProtoPing.connection.send(ping); |
---|
| 49 | |
---|
| 50 | log.insert("<p>Ping sent to " + ProtoPing.TARGET + ".</p>"); |
---|
| 51 | }); |
---|
| 52 | |
---|
| 53 | document.observe('strophe:disconnected', function () { |
---|
| 54 | log.insert('<p>Disconnected.</p>'); |
---|
| 55 | }); |
---|
| 56 | }); |
---|