dojo-ping.js
1.82 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
var DojoPing = {
BOSH_SERVICE: '/xmpp-httpbind',
TARGET: 'jabber.org',
connection: null
};
dojo.addOnLoad(function () {
dojo.connect(dojo.byId('connect'), "click", function (e) {
var jid = dojo.attr(dojo.byId('jid'), 'value');
var pass_node = dojo.byId('pass');
var pass = dojo.attr(pass_node, 'value');
dojo.attr(pass_node, 'value', '');
DojoPing.connection = new Strophe.Connection(DojoPing.BOSH_SERVICE);
dojo.place("<p>Connecting...</p>", "log");
DojoPing.connection.connect(jid, pass, function (status) {
if (status === Strophe.Status.CONNECTED) {
dojo.publish('connected');
} else if (status === Strophe.Status.DISCONNECTED) {
dojo.publish('disconnected');
}
});
});
dojo.subscribe('connected', function () {
dojo.place("<p>Connected.</p>", "log");
var ping = $iq({to: DojoPing.TARGET, type: 'get'})
.c('query', {xmlns: Strophe.NS.DISCO_ITEMS});
var sent_stamp = new Date();
DojoPing.connection.sendIQ(ping, function (iq) {
var elapsed = new Date() - sent_stamp;
// use dojo.query on incoming stanza
var items = dojo.query('items', iq);
console.log(items);
dojo.place("<p>Disco#items response received after " +
elapsed + "ms." + DojoPing.TARGET + " reports " +
"it has " + items + " disco items.</p>", "log");
DojoPing.connection.disconnect();
});
DojoPing.connection.send(ping);
dojo.place("<p>Ping sent to " + DojoPing.TARGET + ".</p>", "log");
});
dojo.subscribe('disconnected', function () {
dojo.place("<p>Disconnected.</p>", "log");
});
});