Skip to content

Commit 4ae8a2e

Browse files
committed
Update strophe.ping.js, add XMPP ping handler
1 parent 8d0c370 commit 4ae8a2e

3 files changed

Lines changed: 97 additions & 53 deletions

File tree

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<script type="application/javascript" src="js/lib/strophe/strophe.ibb.js"></script>
2222
<script type="application/javascript" src="js/lib/strophe/strophe.si-filetransfer.js"></script>
2323
<script type="application/javascript" src="js/lib/strophe/strophe.muc.js"></script>
24+
<script type="application/javascript" src="js/lib/strophe/strophe.ping.js"></script>
2425
<script type="application/javascript" src="js/lib/crypto-js.js"></script>
2526
<script type="application/javascript" src="js/lib/salsa20.js"></script>
2627
<script type="application/javascript" src="js/lib/eventemitter.js"></script>

js/etc/xmpp.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,11 +381,16 @@ $(window).ready(function() {
381381

382382
// Executed (manually) after connection.
383383
var afterConnect = function() {
384-
$('.conversationName').animate({ 'background-color': '#bb7a20' });
384+
Cryptodog.xmpp.connection.ping.addPingHandler(function(ping) {
385+
Cryptodog.xmpp.connection.ping.pong(ping);
386+
return true;
387+
});
385388

386389
Cryptodog.xmpp.connection.ibb.addIBBHandler(Cryptodog.otr.ibbHandler);
387390
Cryptodog.xmpp.connection.si_filetransfer.addFileHandler(Cryptodog.otr.fileHandler);
388391

392+
$('.conversationName').animate({ 'background-color': '#bb7a20' });
393+
389394
Cryptodog.xmpp.sendStatus();
390395
Cryptodog.xmpp.sendPublicKey();
391396
Cryptodog.xmpp.requestPublicKey();

js/lib/strophe/strophe.ping.js

Lines changed: 90 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,91 @@
1-
Strophe.addConnectionPlugin('connectionmanager', {
2-
3-
pingTime: 12000, // the time in ms between each ping
4-
timeoutTime: 6000, // the time in ms to wait for a ping to return
5-
pingInterval: null,
6-
connection: null,
7-
failedPings: 0,
8-
9-
init: function(connection) {
10-
this.connection = connection
11-
},
12-
13-
// Called automatically by Strophe when the connection status changes
14-
statusChanged: function(status) {
15-
var self = this
16-
switch(status) {
17-
case Strophe.Status.CONNECTED:
18-
case Strophe.Status.ATTACHED:
19-
// start monitoring the connection
20-
clearInterval(this.pingInterval)
21-
this.pingInterval = setInterval(function(){ self.pingServer() }, this.pingTime)
22-
break
23-
case Strophe.Status.DISCONNECTED:
24-
// stop monitoring the connection
25-
this.failedPings = 0
26-
clearInterval(this.pingInterval)
27-
break
28-
}
29-
},
30-
31-
pingServer: function() {
32-
var self = this
33-
var stanza = $iq({
34-
id: "ping",
35-
type: "get",
36-
to: this.connection.domain
37-
}).c("ping", {xmlns: 'urn:xmpp:ping'})
38-
39-
this.connection.sendIQ(stanza, null, function(){ self.requestTimedOut() }, this.timeoutTime)
40-
},
41-
42-
requestTimedOut: function() {
43-
this.failedPings++
44-
console.log('FP')
45-
if (this.failedPings === 2) {
46-
this.failedPings = 0
47-
clearInterval(this.pingInterval)
48-
console.log('FP!')
49-
this.connection.disconnect()
50-
}
51-
}
1+
/*
2+
* Based on Ping Strophejs plugins (https://github.com/metajack/strophejs-plugins/tree/master/ping)
3+
* This plugin is distributed under the terms of the MIT licence.
4+
* Please see the LICENCE file for details.
5+
*
6+
* Copyright (c) Markus Kohlhase, 2010
7+
* Refactored by Pavel Lang, 2011
8+
* AMD Support added by Thierry
9+
*/
10+
/**
11+
* File: strophe.ping.js
12+
* A Strophe plugin for XMPP Ping ( http://xmpp.org/extensions/xep-0199.html )
13+
*/
14+
(function (root, factory) {
15+
if (typeof define === 'function' && define.amd) {
16+
// AMD. Register as an anonymous module.
17+
define([
18+
"strophe.js"
19+
], function (Strophe) {
20+
factory(
21+
Strophe.Strophe,
22+
Strophe.$build,
23+
Strophe.$iq ,
24+
Strophe.$msg,
25+
Strophe.$pres
26+
);
27+
return Strophe;
28+
});
29+
} else {
30+
// Browser globals
31+
factory(
32+
root.Strophe,
33+
root.$build,
34+
root.$iq ,
35+
root.$msg,
36+
root.$pres
37+
);
38+
}
39+
}(this, function (Strophe, $build, $iq, $msg, $pres) {
40+
Strophe.addConnectionPlugin('ping', {
41+
_c: null,
5242

53-
})
43+
// called by the Strophe.Connection constructor
44+
init: function(conn) {
45+
this._c = conn;
46+
Strophe.addNamespace('PING', "urn:xmpp:ping");
47+
},
48+
49+
/**
50+
* Function: ping
51+
*
52+
* Parameters:
53+
* (String) to - The JID you want to ping
54+
* (Function) success - Callback function on success
55+
* (Function) error - Callback function on error
56+
* (Integer) timeout - Timeout in milliseconds
57+
*/
58+
ping: function(jid, success, error, timeout) {
59+
var id = this._c.getUniqueId('ping');
60+
var iq = $iq({type: 'get', to: jid, id: id}).c(
61+
'ping', {xmlns: Strophe.NS.PING});
62+
this._c.sendIQ(iq, success, error, timeout);
63+
},
64+
65+
/**
66+
* Function: pong
67+
*
68+
* Parameters:
69+
* (Object) ping - The ping stanza from the server.
70+
*/
71+
pong: function(ping) {
72+
var from = ping.getAttribute('from');
73+
var id = ping.getAttribute('id');
74+
var iq = $iq({type: 'result', to: from,id: id});
75+
this._c.sendIQ(iq);
76+
},
77+
78+
/**
79+
* Function: addPingHandler
80+
*
81+
* Parameters:
82+
* (Function) handler - Ping handler
83+
*
84+
* Returns:
85+
* A reference to the handler that can be used to remove it.
86+
*/
87+
addPingHandler: function(handler) {
88+
return this._c.addHandler(handler, Strophe.NS.PING, "iq", "get");
89+
}
90+
});
91+
}));

0 commit comments

Comments
 (0)