@reactoo/watchtogether-sdk-js 2.7.38-beta.1 → 2.7.38-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reactoo/watchtogether-sdk-js",
3
- "version": "2.7.38-beta.1",
3
+ "version": "2.7.38-beta.4",
4
4
  "description": "Javascript SDK for Reactoo",
5
5
  "main": "src/index.js",
6
6
  "unpkg": "dist/watchtogether-sdk.min.js",
package/src/models/iot.js CHANGED
@@ -6,13 +6,14 @@ let iot = function () {
6
6
  let keepAliveIntervalId = null;
7
7
  let shouldBeConnected = false;
8
8
  let isReconnecting = false;
9
- let failureCount = 0;
9
+ let interruptCount = 0;
10
10
 
11
11
  return {
12
12
 
13
13
  __promise: null,
14
14
 
15
15
  __updateCredentials: () => {
16
+ interruptCount = 0;
16
17
  isReconnecting = true;
17
18
  return this.iot.getCredentials()
18
19
  .then(response => this.__privates.iot.updateWebSocketCredentials(
@@ -56,6 +57,10 @@ let iot = function () {
56
57
  this.__privates.iot.log('MQTT client disconnect');
57
58
  },
58
59
  onInterrupt: () => {
60
+ if(shouldBeConnected && !isReconnecting && !document.hidden && interruptCount > 10) {
61
+ this.iot.__updateCredentials();
62
+ }
63
+ interruptCount++;
59
64
  console.log('MQTT client interrupt');
60
65
  this.__privates.iot.log('MQTT client interrupt');
61
66
  },
@@ -69,11 +74,7 @@ let iot = function () {
69
74
  },
70
75
  onConnectionFailure: () => {
71
76
 
72
- if(shouldBeConnected && !isReconnecting && failureCount > 10) {
73
- this.iot.__updateCredentials();
74
- }
75
77
 
76
- //failureCount++;
77
78
 
78
79
  console.log('MQTT client connection_failure');
79
80
 
@@ -81,6 +82,10 @@ let iot = function () {
81
82
  },
82
83
 
83
84
  iotLogin: (subscribeToSuggestedTopics = true) => {
85
+
86
+ interruptCount = 0;
87
+ shouldBeConnected = true;
88
+
84
89
  this.iot.__promise = new Promise((resolve, reject) => {
85
90
  this.iot.getCredentials()
86
91
  .then(response => {
@@ -104,10 +109,8 @@ let iot = function () {
104
109
  let __currentTopicsCopy = new Set(__currentTopics); // Keep this line
105
110
  __currentTopics.clear();
106
111
 
107
- shouldBeConnected = true;
108
-
109
112
  this.iot.setupVisibilityChangeListener();
110
- //this.iot.setupKeepAliveInterval();
113
+ this.iot.setupKeepAliveInterval();
111
114
  this.iot.$on('connect', this.iot.onConnect, this);
112
115
  this.iot.$on('closed', this.iot.onClosed, this);
113
116
  this.iot.$on('error', this.iot.onError, this);
@@ -138,16 +141,18 @@ let iot = function () {
138
141
  },
139
142
 
140
143
  iotLogout: (keepCurrentTopics = false) => {
144
+
141
145
  if(!keepCurrentTopics) {
142
146
  __currentTopics.clear();
143
147
  }
144
148
 
149
+ interruptCount = 0;
145
150
  shouldBeConnected = false;
146
151
 
147
152
  return this.__privates.iot.disconnect()
148
153
  .then(() => {
149
154
  this.iot.disableVisibilityChangeListener();
150
- //this.iot.disableKeepAliveInterval();
155
+ this.iot.disableKeepAliveInterval();
151
156
  this.iot.$off('connect', this.iot.onConnect, this);
152
157
  this.iot.$off('closed', this.iot.onClosed, this);
153
158
  this.iot.$off('error', this.iot.onError, this);
@@ -212,7 +217,9 @@ let iot = function () {
212
217
 
213
218
  setupKeepAliveInterval: () => {
214
219
  clearInterval(keepAliveIntervalId);
215
- keepAliveIntervalId = setInterval(this.iot.checkConnection, 30000);
220
+ keepAliveIntervalId = setInterval(() => {
221
+ this.__privates.iot.keepAliveMessage();
222
+ }, 30000);
216
223
  },
217
224
 
218
225
  disableKeepAliveInterval: () => {
@@ -7,7 +7,6 @@ class Iot {
7
7
  Object.assign(this, emitter());
8
8
  this.decoder = new TextDecoder('utf-8');
9
9
  this.log = Iot.noop;
10
- // Remove: this.debugFlag = enableDebugFlag;
11
10
  this.credentialsExpirationCheckIntervalId = null;
12
11
  this.currentCredentialsExpirationStamp = null;
13
12
  this.lastConnectParams = null;
@@ -252,9 +251,7 @@ class Iot {
252
251
  this.log('iot updateWebSocketCredentials');
253
252
  this.lastConnectParams = {...this.lastConnectParams, accessKeyId, secretAccessKey, sessionToken, expiration };
254
253
  const currentTopics = new Set(this.subscribedTopics);
255
-
256
254
  // disconnect is part of connect process
257
-
258
255
  return this.connect(
259
256
  this.lastConnectParams.apiMqttUrl,
260
257
  this.lastConnectParams.apiMqttClientId,
@@ -319,6 +316,24 @@ class Iot {
319
316
  }
320
317
  });
321
318
  }
319
+
320
+ keepAliveMessage() {
321
+ if (this.subscribedTopics.size === 0) {
322
+ this.log('No subscribed topics available for keep alive message');
323
+ return;
324
+ }
325
+ // Find a suitable topic for the connection check
326
+ const suitableTopic = Array.from(this.subscribedTopics).find(topic => topic.indexOf('user') > -1);
327
+ if (!suitableTopic) {
328
+ this.log('No suitable topic found for keep alive message');
329
+ return;
330
+ }
331
+ const testMessage = {
332
+ type: 'keep_alive',
333
+ timestamp: Date.now()
334
+ };
335
+ this.send(suitableTopic, testMessage);
336
+ }
322
337
  }
323
338
 
324
339
  export default Iot;