@reactoo/watchtogether-sdk-js 2.7.55 → 2.7.57
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
package/src/models/iot.js
CHANGED
|
@@ -44,37 +44,42 @@ let iot = function () {
|
|
|
44
44
|
|
|
45
45
|
},
|
|
46
46
|
|
|
47
|
-
onConnect: () => {
|
|
47
|
+
onConnect: (data, connectionId) => {
|
|
48
48
|
this.__privates.iot.log('MQTT client connected');
|
|
49
49
|
},
|
|
50
|
-
onClosed: () => {
|
|
50
|
+
onClosed: (data, connectionId) => {
|
|
51
51
|
this.__privates.iot.log('MQTT client closed');
|
|
52
52
|
if(shouldBeConnected && !isReconnecting) {
|
|
53
53
|
this.__privates.iot.log('Connection unexpectedly closed, reconnecting...');
|
|
54
54
|
this.iot.__updateCredentials();
|
|
55
55
|
}
|
|
56
56
|
},
|
|
57
|
-
onError: () => {
|
|
57
|
+
onError: (data, connectionId) => {
|
|
58
58
|
this.__privates.iot.log('MQTT client error');
|
|
59
59
|
},
|
|
60
|
-
onDisconnect: () => {
|
|
60
|
+
onDisconnect: (data, connectionId) => {
|
|
61
61
|
this.__privates.iot.log('MQTT client disconnect');
|
|
62
62
|
},
|
|
63
|
-
onInterrupt: () => {
|
|
64
|
-
if(shouldBeConnected && !isReconnecting &&
|
|
63
|
+
onInterrupt: (data, connectionId) => {
|
|
64
|
+
if(shouldBeConnected && !isReconnecting && interruptCount > 10) {
|
|
65
65
|
this.__privates.iot.log('Interrupt count exceeded, reconnecting...');
|
|
66
66
|
this.iot.__updateCredentials();
|
|
67
67
|
}
|
|
68
68
|
interruptCount++;
|
|
69
69
|
this.__privates.iot.log('MQTT client interrupt');
|
|
70
70
|
},
|
|
71
|
-
onResume: () => {
|
|
71
|
+
onResume: (data, connectionId) => {
|
|
72
72
|
this.__privates.iot.log('MQTT client resume');
|
|
73
73
|
},
|
|
74
|
-
onConnectionSuccess: () => {
|
|
74
|
+
onConnectionSuccess: (data, connectionId) => {
|
|
75
75
|
this.__privates.iot.log('MQTT client connection_success');
|
|
76
76
|
},
|
|
77
|
-
onConnectionFailure: () => {
|
|
77
|
+
onConnectionFailure: (data, connectionId) => {
|
|
78
|
+
if(shouldBeConnected && !isReconnecting && interruptCount > 10) {
|
|
79
|
+
this.__privates.iot.log('Interrupt count exceeded, reconnecting...');
|
|
80
|
+
this.iot.__updateCredentials();
|
|
81
|
+
}
|
|
82
|
+
interruptCount++;
|
|
78
83
|
this.__privates.iot.log('MQTT client connection_failure');
|
|
79
84
|
},
|
|
80
85
|
|
|
@@ -2,6 +2,7 @@ import { mqtt, iot } from 'aws-iot-device-sdk-v2';
|
|
|
2
2
|
console.log('Worker: Starting up');
|
|
3
3
|
|
|
4
4
|
let connection = null;
|
|
5
|
+
let currentConnectionId = null;
|
|
5
6
|
|
|
6
7
|
self.onmessage = function(event) {
|
|
7
8
|
const { type, params, topic, message } = event.data;
|
|
@@ -48,16 +49,18 @@ function connect(params) {
|
|
|
48
49
|
configBuilder.with_reconnect_min_sec(1);
|
|
49
50
|
|
|
50
51
|
const config = configBuilder.build();
|
|
51
|
-
|
|
52
52
|
const client = new mqtt.MqttClient();
|
|
53
|
+
|
|
53
54
|
connection = client.new_connection(config);
|
|
55
|
+
// generate random connection id
|
|
56
|
+
currentConnectionId = Math.floor(Math.random() * 1000000);
|
|
54
57
|
|
|
55
|
-
setupConnectionListeners();
|
|
58
|
+
setupConnectionListeners(currentConnectionId);
|
|
56
59
|
|
|
57
60
|
connection.connect()
|
|
58
61
|
.then(() => {
|
|
59
62
|
console.log('Worker: Connection successful');
|
|
60
|
-
self.postMessage({ type: 'connect_result', data: {success: true} });
|
|
63
|
+
self.postMessage({ type: 'connect_result', data: {success: true, connectionId: currentConnectionId} });
|
|
61
64
|
})
|
|
62
65
|
.catch((error) => {
|
|
63
66
|
console.error('Worker: Connection failed', error);
|
|
@@ -120,43 +123,69 @@ function send(topic, message) {
|
|
|
120
123
|
}
|
|
121
124
|
}
|
|
122
125
|
|
|
123
|
-
function setupConnectionListeners() {
|
|
126
|
+
function setupConnectionListeners(connectionId) {
|
|
124
127
|
connection.on('connect', () => {
|
|
125
|
-
|
|
128
|
+
if(connectionId !== currentConnectionId) {
|
|
129
|
+
console.log('Worker: Connection Id mismatch, ignoring connect event');
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
self.postMessage({ type: 'connect', connectionId: connectionId});
|
|
126
133
|
});
|
|
127
134
|
|
|
128
135
|
connection.on('disconnect', () => {
|
|
129
|
-
|
|
136
|
+
if(connectionId !== currentConnectionId) {
|
|
137
|
+
console.log('Worker: Connection Id mismatch, ignoring disconnect event');
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
self.postMessage({ type: 'disconnect', connectionId: connectionId });
|
|
130
141
|
});
|
|
131
142
|
|
|
132
143
|
connection.on('error', (error) => {
|
|
144
|
+
if(connectionId !== currentConnectionId) {
|
|
145
|
+
console.log('Worker: Connection Id mismatch, ignoring error event');
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
133
148
|
self.postMessage({ type: 'error', data: error });
|
|
134
149
|
});
|
|
135
150
|
|
|
136
151
|
connection.on('interrupt', (error) => {
|
|
137
|
-
|
|
152
|
+
if(connectionId !== currentConnectionId) {
|
|
153
|
+
console.log('Worker: Connection Id mismatch, ignoring interrupt event');
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
self.postMessage({ type: 'interrupt', data: error, connectionId: connectionId });
|
|
138
157
|
});
|
|
139
158
|
|
|
140
159
|
connection.on('resume', (error) => {
|
|
141
|
-
|
|
160
|
+
if(connectionId !== currentConnectionId) {
|
|
161
|
+
console.log('Worker: Connection Id mismatch, ignoring resume event');
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
self.postMessage({ type: 'resume', data: error, connectionId: connectionId });
|
|
142
165
|
});
|
|
143
166
|
|
|
144
167
|
connection.on('message', (topic, payload) => {
|
|
145
|
-
|
|
168
|
+
if(connectionId !== currentConnectionId) {
|
|
169
|
+
console.log('Worker: Connection Id mismatch, ignoring message event');
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
self.postMessage({ type: 'message', data: { topic, payload }, connectionId: connectionId });
|
|
146
173
|
});
|
|
147
174
|
|
|
148
175
|
connection.on('connection_success', (error) => {
|
|
149
|
-
|
|
176
|
+
if(connectionId !== currentConnectionId) {
|
|
177
|
+
console.log('Worker: Connection Id mismatch, ignoring connection_success event');
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
self.postMessage({ type: 'connection_success', data: error, connectionId: connectionId });
|
|
150
181
|
});
|
|
151
182
|
|
|
152
183
|
connection.on('connection_failure', (error) => {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
console.error('Worker: Uncaught error', error);
|
|
159
|
-
self.postMessage({ type: 'uncaught_error', error: error.message });
|
|
184
|
+
if(connectionId !== currentConnectionId) {
|
|
185
|
+
console.log('Worker: Connection Id mismatch, ignoring connection_failure event');
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
self.postMessage({ type: 'connection_failure', data: error, connectionId: connectionId });
|
|
160
189
|
});
|
|
161
190
|
}
|
|
162
191
|
|
package/src/modules/wt-iot2.js
CHANGED
|
@@ -11,6 +11,7 @@ class Iot {
|
|
|
11
11
|
this.currentCredentialsExpirationStamp = null;
|
|
12
12
|
this.lastConnectParams = null;
|
|
13
13
|
this.subscribedTopics = new Set();
|
|
14
|
+
this.currentConnectionId = null;
|
|
14
15
|
this.initWorker();
|
|
15
16
|
|
|
16
17
|
if (enableDebugFlag) {
|
|
@@ -51,6 +52,7 @@ class Iot {
|
|
|
51
52
|
clearTimeout(timeoutId);
|
|
52
53
|
this.off('worker:connect_result', handleConnectResult);
|
|
53
54
|
if (event.success) {
|
|
55
|
+
this.currentConnectionId = event.connectionId;
|
|
54
56
|
resolve();
|
|
55
57
|
} else {
|
|
56
58
|
reject(new Error(event.error));
|
|
@@ -160,7 +162,7 @@ class Iot {
|
|
|
160
162
|
}
|
|
161
163
|
|
|
162
164
|
handleWorkerMessage(event) {
|
|
163
|
-
const { type, data } = event.data;
|
|
165
|
+
const { type, data, connectionId } = event.data;
|
|
164
166
|
switch (type) {
|
|
165
167
|
case 'message':
|
|
166
168
|
this.handleMessage(data.topic, new Uint8Array(data.payload));
|
|
@@ -172,7 +174,7 @@ class Iot {
|
|
|
172
174
|
case 'resume':
|
|
173
175
|
case 'connection_success':
|
|
174
176
|
case 'connection_failure':
|
|
175
|
-
this.emit(type, data);
|
|
177
|
+
this.emit(type, data, connectionId);
|
|
176
178
|
break;
|
|
177
179
|
case 'connect_result':
|
|
178
180
|
case 'disconnect_result':
|