1 | var DojoPing = { |
---|
2 | BOSH_SERVICE: '/xmpp-httpbind', |
---|
3 | TARGET: 'jabber.org', |
---|
4 | connection: null |
---|
5 | }; |
---|
6 | |
---|
7 | dojo.addOnLoad(function () { |
---|
8 | dojo.connect(dojo.byId('connect'), "click", function (e) { |
---|
9 | var jid = dojo.attr(dojo.byId('jid'), 'value'); |
---|
10 | var pass_node = dojo.byId('pass'); |
---|
11 | var pass = dojo.attr(pass_node, 'value'); |
---|
12 | dojo.attr(pass_node, 'value', ''); |
---|
13 | |
---|
14 | DojoPing.connection = new Strophe.Connection(DojoPing.BOSH_SERVICE); |
---|
15 | |
---|
16 | dojo.place("<p>Connecting...</p>", "log"); |
---|
17 | |
---|
18 | DojoPing.connection.connect(jid, pass, function (status) { |
---|
19 | if (status === Strophe.Status.CONNECTED) { |
---|
20 | dojo.publish('connected'); |
---|
21 | } else if (status === Strophe.Status.DISCONNECTED) { |
---|
22 | dojo.publish('disconnected'); |
---|
23 | } |
---|
24 | }); |
---|
25 | }); |
---|
26 | |
---|
27 | dojo.subscribe('connected', function () { |
---|
28 | dojo.place("<p>Connected.</p>", "log"); |
---|
29 | |
---|
30 | var ping = $iq({to: DojoPing.TARGET, type: 'get'}) |
---|
31 | .c('query', {xmlns: Strophe.NS.DISCO_ITEMS}); |
---|
32 | |
---|
33 | var sent_stamp = new Date(); |
---|
34 | DojoPing.connection.sendIQ(ping, function (iq) { |
---|
35 | var elapsed = new Date() - sent_stamp; |
---|
36 | |
---|
37 | // use dojo.query on incoming stanza |
---|
38 | var items = dojo.query('items', iq); |
---|
39 | console.log(items); |
---|
40 | |
---|
41 | dojo.place("<p>Disco#items response received after " + |
---|
42 | elapsed + "ms." + DojoPing.TARGET + " reports " + |
---|
43 | "it has " + items + " disco items.</p>", "log"); |
---|
44 | |
---|
45 | DojoPing.connection.disconnect(); |
---|
46 | }); |
---|
47 | |
---|
48 | DojoPing.connection.send(ping); |
---|
49 | |
---|
50 | dojo.place("<p>Ping sent to " + DojoPing.TARGET + ".</p>", "log"); |
---|
51 | }); |
---|
52 | |
---|
53 | dojo.subscribe('disconnected', function () { |
---|
54 | dojo.place("<p>Disconnected.</p>", "log"); |
---|
55 | }); |
---|
56 | }); |
---|