|
@@ -3,7 +3,6 @@ import { $iq, Strophe } from 'strophe.js';
|
3
|
3
|
|
4
|
4
|
import ConnectionPlugin from './ConnectionPlugin';
|
5
|
5
|
|
6
|
|
-
|
7
|
6
|
const logger = getLogger('modules/xmpp/strophe.ping');
|
8
|
7
|
|
9
|
8
|
/**
|
|
@@ -23,12 +22,35 @@ const PING_DEFAULT_TIMEOUT = 5000;
|
23
|
22
|
*/
|
24
|
23
|
const PING_DEFAULT_THRESHOLD = 2;
|
25
|
24
|
|
|
25
|
+export interface IPingOptions {
|
|
26
|
+ interval?: number;
|
|
27
|
+ threshold?: number;
|
|
28
|
+ timeout?: number;
|
|
29
|
+}
|
|
30
|
+
|
|
31
|
+export interface IPingConnectionPluginOptions {
|
|
32
|
+ getTimeSinceLastServerResponse: () => number;
|
|
33
|
+ onPingThresholdExceeded: () => void;
|
|
34
|
+ pingOptions?: IPingOptions;
|
|
35
|
+}
|
|
36
|
+
|
26
|
37
|
/**
|
27
|
38
|
* XEP-0199 ping plugin.
|
28
|
39
|
*
|
29
|
40
|
* Registers "urn:xmpp:ping" namespace under Strophe.NS.PING.
|
30
|
41
|
*/
|
31
|
42
|
export default class PingConnectionPlugin extends ConnectionPlugin {
|
|
43
|
+ failedPings: number;
|
|
44
|
+ _onPingThresholdExceeded: () => void;
|
|
45
|
+ _getTimeSinceLastServerResponse: () => number;
|
|
46
|
+ pingInterval: number;
|
|
47
|
+ pingTimeout: number;
|
|
48
|
+ pingThreshold: number;
|
|
49
|
+ pingTimestampsToKeep: number;
|
|
50
|
+ pingExecIntervals: number[];
|
|
51
|
+ intervalId: number | null;
|
|
52
|
+ _lastServerCheck: number;
|
|
53
|
+
|
32
|
54
|
/**
|
33
|
55
|
* Constructs new object
|
34
|
56
|
* @param {Object} options
|
|
@@ -39,7 +61,7 @@ export default class PingConnectionPlugin extends ConnectionPlugin {
|
39
|
61
|
* @param {Object} options.pingOptions - The ping options if any.
|
40
|
62
|
* @constructor
|
41
|
63
|
*/
|
42
|
|
- constructor({ getTimeSinceLastServerResponse, onPingThresholdExceeded, pingOptions = {} }) {
|
|
64
|
+ constructor({ getTimeSinceLastServerResponse, onPingThresholdExceeded, pingOptions = {} }: IPingConnectionPluginOptions) {
|
43
|
65
|
super();
|
44
|
66
|
this.failedPings = 0;
|
45
|
67
|
this._onPingThresholdExceeded = onPingThresholdExceeded;
|
|
@@ -60,7 +82,7 @@ export default class PingConnectionPlugin extends ConnectionPlugin {
|
60
|
82
|
* Initializes the plugin. Method called by Strophe.
|
61
|
83
|
* @param connection Strophe connection instance.
|
62
|
84
|
*/
|
63
|
|
- init(connection) {
|
|
85
|
+ init(connection: Strophe.Connection): void {
|
64
|
86
|
super.init(connection);
|
65
|
87
|
Strophe.addNamespace('PING', 'urn:xmpp:ping');
|
66
|
88
|
}
|
|
@@ -75,7 +97,7 @@ export default class PingConnectionPlugin extends ConnectionPlugin {
|
75
|
97
|
* @param timeout ms how long are we going to wait for the response. On
|
76
|
98
|
* timeout <tt>error<//t> callback is called with undefined error argument.
|
77
|
99
|
*/
|
78
|
|
- ping(jid, success, error, timeout) {
|
|
100
|
+ ping(jid: string, success: (result: any) => void, error: (err: any) => void, timeout: number): void {
|
79
|
101
|
this._addPingExecutionTimestamp();
|
80
|
102
|
|
81
|
103
|
const iq = $iq({
|
|
@@ -96,7 +118,7 @@ export default class PingConnectionPlugin extends ConnectionPlugin {
|
96
|
118
|
* must be called before starting a new one.
|
97
|
119
|
* @param remoteJid remote JID to which ping requests will be sent to.
|
98
|
120
|
*/
|
99
|
|
- startInterval(remoteJid) {
|
|
121
|
+ startInterval(remoteJid: string): void {
|
100
|
122
|
clearInterval(this.intervalId);
|
101
|
123
|
this.intervalId = window.setInterval(() => {
|
102
|
124
|
|
|
@@ -140,7 +162,7 @@ export default class PingConnectionPlugin extends ConnectionPlugin {
|
140
|
162
|
/**
|
141
|
163
|
* Stops current "ping" interval task.
|
142
|
164
|
*/
|
143
|
|
- stopInterval() {
|
|
165
|
+ stopInterval(): void {
|
144
|
166
|
if (this.intervalId) {
|
145
|
167
|
window.clearInterval(this.intervalId);
|
146
|
168
|
this.intervalId = null;
|
|
@@ -153,7 +175,7 @@ export default class PingConnectionPlugin extends ConnectionPlugin {
|
153
|
175
|
* Adds the current time to the array of send ping timestamps.
|
154
|
176
|
* @private
|
155
|
177
|
*/
|
156
|
|
- _addPingExecutionTimestamp() {
|
|
178
|
+ _addPingExecutionTimestamp(): void {
|
157
|
179
|
this.pingExecIntervals.push(new Date().getTime());
|
158
|
180
|
|
159
|
181
|
// keep array length to PING_TIMESTAMPS_TO_KEEP
|
|
@@ -170,7 +192,7 @@ export default class PingConnectionPlugin extends ConnectionPlugin {
|
170
|
192
|
*
|
171
|
193
|
* @returns {int} the time ping was suspended, if it was not 0 is returned.
|
172
|
194
|
*/
|
173
|
|
- getPingSuspendTime() {
|
|
195
|
+ getPingSuspendTime(): number {
|
174
|
196
|
const pingIntervals = this.pingExecIntervals.slice();
|
175
|
197
|
|
176
|
198
|
// we need current time, as if ping was sent now
|