Переглянути джерело

ref(xmpp): adjust last success time tracker for bosh and websocket (#1053)

dev1
Paweł Domas 5 роки тому
джерело
коміт
86387e599e
Аккаунт користувача з таким Email не знайдено

+ 0
- 41
modules/xmpp/StropheBoshLastSuccess.js Переглянути файл

@@ -1,41 +0,0 @@
1
-/**
2
- * Class attaches to Strophe BOSH connection and tracks the time of last successful request.
3
- * It does that by overriding {@code nextValidRid} method and tracking how the RID value changes.
4
- * A request was successful if the number has increased by 1 since the last time the method was called.
5
- */
6
-export default class LastRequestTracker {
7
-    /**
8
-     * Initializes new instance.
9
-     */
10
-    constructor() {
11
-        this._nextValidRid = null;
12
-        this._lastSuccess = null;
13
-    }
14
-
15
-    /**
16
-     * Starts tracking requests on the given connection.
17
-     *
18
-     * @param {Object} stropheConnection - Strophe connection instance.
19
-     */
20
-    startTracking(stropheConnection) {
21
-        stropheConnection.nextValidRid = rid => {
22
-            // Just before connect and on disconnect RID will get assigned a new random value.
23
-            // A request was successful only when the value got increased exactly by 1.
24
-            if (this._nextValidRid === rid - 1) {
25
-                this._lastSuccess = new Date().getTime();
26
-            }
27
-            this._nextValidRid = rid;
28
-        };
29
-    }
30
-
31
-    /**
32
-     * Returns how many milliseconds have passed since the last successful BOSH request.
33
-     *
34
-     * @returns {number|null}
35
-     */
36
-    getTimeSinceLastSuccess() {
37
-        return this._lastSuccess
38
-            ? new Date().getTime() - this._lastSuccess
39
-            : null;
40
-    }
41
-}

+ 36
- 0
modules/xmpp/StropheLastSuccess.js Переглянути файл

@@ -0,0 +1,36 @@
1
+/**
2
+ * Attaches to the {@link Strophe.Connection.rawInput} which is called whenever any data is received from the server.
3
+ */
4
+export default class LastRequestTracker {
5
+    /**
6
+     * Initializes new instance.
7
+     */
8
+    constructor() {
9
+        this._lastSuccess = null;
10
+    }
11
+
12
+    /**
13
+     * Starts tracking requests on the given connection.
14
+     *
15
+     * @param {Object} stropheConnection - Strophe connection instance.
16
+     */
17
+    startTracking(stropheConnection) {
18
+        const originalRawInput = stropheConnection.rawInput;
19
+
20
+        stropheConnection.rawInput = function(...args) {
21
+            this._lastSuccess = Date.now();
22
+            originalRawInput.apply(stropheConnection, args);
23
+        };
24
+    }
25
+
26
+    /**
27
+     * Returns how many milliseconds have passed since the last successful BOSH request.
28
+     *
29
+     * @returns {number|null}
30
+     */
31
+    getTimeSinceLastSuccess() {
32
+        return this._lastSuccess
33
+            ? Date.now() - this._lastSuccess
34
+            : null;
35
+    }
36
+}

+ 5
- 9
modules/xmpp/XmppConnection.js Переглянути файл

@@ -5,7 +5,7 @@ import 'strophejs-plugin-stream-management';
5 5
 import Listenable from '../util/Listenable';
6 6
 import { getJitterDelay } from '../util/Retry';
7 7
 
8
-import LastSuccessTracker from './StropheBoshLastSuccess';
8
+import LastSuccessTracker from './StropheLastSuccess';
9 9
 
10 10
 const logger = getLogger(__filename);
11 11
 
@@ -64,10 +64,8 @@ export default class XmppConnection extends Listenable {
64 64
         // The default maxRetries is 5, which is too long.
65 65
         this._stropheConn.maxRetries = 3;
66 66
 
67
-        if (!this._usesWebsocket) {
68
-            this._lastSuccessTracker = new LastSuccessTracker();
69
-            this._lastSuccessTracker.startTracking(this._stropheConn);
70
-        }
67
+        this._lastSuccessTracker = new LastSuccessTracker();
68
+        this._lastSuccessTracker.startTracking(this._stropheConn);
71 69
     }
72 70
 
73 71
     /**
@@ -279,10 +277,8 @@ export default class XmppConnection extends Listenable {
279 277
      *
280 278
      * @returns {number|null}
281 279
      */
282
-    getTimeSinceLastBOSHSuccess() {
283
-        return this._lastSuccessTracker
284
-            ? this._lastSuccessTracker.getTimeSinceLastSuccess()
285
-            : null;
280
+    getTimeSinceLastSuccess() {
281
+        return this._lastSuccessTracker.getTimeSinceLastSuccess();
286 282
     }
287 283
 
288 284
     /**

+ 1
- 1
modules/xmpp/xmpp.js Переглянути файл

@@ -668,7 +668,7 @@ export default class XMPP extends Listenable {
668 668
         /* eslint-disable camelcase */
669 669
         // check for possible suspend
670 670
         details.suspend_time = this.connection.ping.getPingSuspendTime();
671
-        details.time_since_last_success = this.connection.getTimeSinceLastBOSHSuccess();
671
+        details.time_since_last_success = this.connection.getTimeSinceLastSuccess();
672 672
         /* eslint-enable camelcase */
673 673
 
674 674
         return details;

Завантаження…
Відмінити
Зберегти