@reactoo/watchtogether-sdk-js 2.7.21 → 2.7.24
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/.babelrc +5 -1
- package/dist/watchtogether-sdk.js +93702 -5514
- package/dist/watchtogether-sdk.js.map +1 -0
- package/dist/watchtogether-sdk.min.js +3 -3
- package/example/index.html +8 -0
- package/package.json +21 -18
- package/src/models/iot.js +102 -38
- package/src/models/room.js +6 -5
- package/src/modules/iot-worker.worker.js +84 -0
- package/src/modules/wt-iot.js +235 -165
- package/src/modules/wt-room.js +26 -4
- package/webpack.config.js +64 -65
- package/src/modules/wt-iot-worker.js +0 -245
|
@@ -1,245 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import {device} from 'aws-iot-device-sdk';
|
|
3
|
-
import emitter from './wt-emitter';
|
|
4
|
-
import {decodeJanusDisplay} from "./wt-utils";
|
|
5
|
-
|
|
6
|
-
class Iot {
|
|
7
|
-
|
|
8
|
-
constructor(enableDebugFlag) {
|
|
9
|
-
Object.assign(this, emitter());
|
|
10
|
-
this.device = null;
|
|
11
|
-
this.decoder = new TextDecoder('utf-8');
|
|
12
|
-
this.connectionActive = false;
|
|
13
|
-
this.log = Iot.noop;
|
|
14
|
-
this.debugFlag = enableDebugFlag;
|
|
15
|
-
this.credentialsExpirationCheckIntervalId = null;
|
|
16
|
-
this.currentCredentialsExpirationStamp = null
|
|
17
|
-
if(enableDebugFlag) {
|
|
18
|
-
this.enableDebug();
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
static noop() {}
|
|
23
|
-
|
|
24
|
-
enableDebug() {
|
|
25
|
-
this.log = console.log.bind(console);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
startCredentialsExpirationCheck(expiration) {
|
|
29
|
-
this.stopCredentialsExpirationCheck();
|
|
30
|
-
this.currentCredentialsExpirationStamp = new Date(expiration).getTime();
|
|
31
|
-
this.credentialsExpirationCheckIntervalId = setInterval(() => {
|
|
32
|
-
const curentTimeStamp = new Date().getTime();
|
|
33
|
-
if(this.currentCredentialsExpirationStamp - curentTimeStamp <= 300000) {
|
|
34
|
-
this.emit('updateCredentials');
|
|
35
|
-
}
|
|
36
|
-
}, 5000);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
stopCredentialsExpirationCheck() {
|
|
40
|
-
clearInterval(this.credentialsExpirationCheckIntervalId);
|
|
41
|
-
this.credentialsExpirationCheckIntervalId = null;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
updateWebSocketCredentials(accessKeyId, secretAccessKey, sessionToken, expiration) {
|
|
45
|
-
if(this.device) {
|
|
46
|
-
this.device.updateWebSocketCredentials(accessKeyId, secretAccessKey, sessionToken);
|
|
47
|
-
this.startCredentialsExpirationCheck(expiration);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
connect(apiMqttUrl, apiMqttClientId, region, accessKeyId, secretAccessKey, sessionToken, expiration, forceDisconnect = false) {
|
|
52
|
-
return this.disconnect(forceDisconnect).then(() => {
|
|
53
|
-
return new Promise((resolve, reject) => {
|
|
54
|
-
this.device = device({
|
|
55
|
-
protocol:'wss',
|
|
56
|
-
clientId: apiMqttClientId,
|
|
57
|
-
region,
|
|
58
|
-
host: apiMqttUrl,
|
|
59
|
-
accessKeyId: accessKeyId,
|
|
60
|
-
secretKey: secretAccessKey,
|
|
61
|
-
sessionToken: sessionToken,
|
|
62
|
-
keepalive: 15,
|
|
63
|
-
maximumReconnectTimeMs: 8000,
|
|
64
|
-
enableMetrics: false,
|
|
65
|
-
debug: this.debugFlag,
|
|
66
|
-
autoResubscribe: true
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
this.startCredentialsExpirationCheck(expiration);
|
|
70
|
-
|
|
71
|
-
let __s = () => {
|
|
72
|
-
this.device?.off('connect', __s);
|
|
73
|
-
this.device?.off('error', __e);
|
|
74
|
-
resolve(this.device)
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
let __e = (e) => {
|
|
78
|
-
this.device?.off('connect', __s);
|
|
79
|
-
this.device?.off('error', __e);
|
|
80
|
-
reject(e);
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
this.device.once('connect', __s);
|
|
84
|
-
this.device.once('error', __e);
|
|
85
|
-
|
|
86
|
-
this.device.on('message', this.__messageCb.bind(this));
|
|
87
|
-
this.device.on('connect', this.__connectCb.bind(this));
|
|
88
|
-
this.device.on('reconnect', this.__reconnectCb.bind(this));
|
|
89
|
-
this.device.on('error', this.__failureCb.bind(this));
|
|
90
|
-
this.device.on('close', this.__closeCb.bind(this));
|
|
91
|
-
this.device.on('offline', this.__offlineCb.bind(this));
|
|
92
|
-
|
|
93
|
-
})
|
|
94
|
-
})
|
|
95
|
-
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
disconnect(force = false) {
|
|
99
|
-
|
|
100
|
-
this.stopCredentialsExpirationCheck();
|
|
101
|
-
|
|
102
|
-
return new Promise((resolve, reject) => {
|
|
103
|
-
if(!this.device) {
|
|
104
|
-
resolve();
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
let __i = null;
|
|
108
|
-
let __c = () => {
|
|
109
|
-
clearTimeout(__i);
|
|
110
|
-
this.device = null;
|
|
111
|
-
resolve();
|
|
112
|
-
};
|
|
113
|
-
__i = setTimeout(__c, 4000);
|
|
114
|
-
this.device.off('message', this.__messageCb.bind(this));
|
|
115
|
-
this.device.off('connect', this.__connectCb.bind(this));
|
|
116
|
-
this.device.off('reconnect', this.__reconnectCb.bind(this));
|
|
117
|
-
this.device.off('error', this.__failureCb.bind(this));
|
|
118
|
-
this.device.off('close', this.__closeCb.bind(this));
|
|
119
|
-
this.device.off('offline', this.__offlineCb.bind(this));
|
|
120
|
-
this.device.end(force, __c);
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
isConnected() {
|
|
125
|
-
return this.connectionActive;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
subscribe(topic) {
|
|
129
|
-
return this.device && this.device.subscribe(topic);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
unsubscribe(topic) {
|
|
133
|
-
return this.device && this.device.unsubscribe(topic);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
send(topic, message) {
|
|
137
|
-
let msg = typeof message === 'object' ? JSON.stringify(message) : message;
|
|
138
|
-
return this.device && this.device.publish(topic, msg);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
__reconnectCb() {
|
|
142
|
-
this.emit('reconnect');
|
|
143
|
-
}
|
|
144
|
-
__connectCb() {
|
|
145
|
-
this.connectionActive = true;
|
|
146
|
-
this.emit('connect');
|
|
147
|
-
}
|
|
148
|
-
__failureCb(err) {
|
|
149
|
-
this.emit('error', err);
|
|
150
|
-
}
|
|
151
|
-
__closeCb(responseObject) {
|
|
152
|
-
this.connectionActive = false;
|
|
153
|
-
this.emit('close');
|
|
154
|
-
}
|
|
155
|
-
__offlineCb(responseObject) {
|
|
156
|
-
this.emit('offline');
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
__messageCb(t, message, packet) {
|
|
160
|
-
const topic = t.split('/');
|
|
161
|
-
let payload = JSON.parse(this.decoder.decode(message));
|
|
162
|
-
|
|
163
|
-
if(payload.display) {
|
|
164
|
-
const decodedDisplay = decodeJanusDisplay(payload.display);
|
|
165
|
-
if(decodedDisplay.userId) {
|
|
166
|
-
payload = {...payload, userId: decodedDisplay.userId, role: decodedDisplay.role, start: decodedDisplay.start};
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
if(topic[0] === 'user') { // user
|
|
171
|
-
const userId = topic[1].replace("_", ':');
|
|
172
|
-
this.emit('message', {userId, ...payload, event: payload.event ? `user:${payload.event}` : 'user'});
|
|
173
|
-
} else if(topic[0] === 'wt') {
|
|
174
|
-
const event = payload.event;
|
|
175
|
-
const roomId = topic[2];
|
|
176
|
-
if(topic[1] === 'room') { // room
|
|
177
|
-
if(
|
|
178
|
-
event === 'message' ||
|
|
179
|
-
event === 'template_updated' ||
|
|
180
|
-
event === 'record_start' ||
|
|
181
|
-
event === 'record_stop' ||
|
|
182
|
-
event === 'record_configured' ||
|
|
183
|
-
event === 'record_livestream_available' ||
|
|
184
|
-
event === 'record_livestream_kick' ||
|
|
185
|
-
event === 'user_update_displayname' ||
|
|
186
|
-
event === 'user_update_avatar' ||
|
|
187
|
-
event === 'user_update_customattributes' ||
|
|
188
|
-
event === 'user_update_privateattributes' ||
|
|
189
|
-
event === 'channel_changed' ||
|
|
190
|
-
event === "instance_homepage_changed" ||
|
|
191
|
-
event === "instance_settings_changed" ||
|
|
192
|
-
event === "externalmix_changed" ||
|
|
193
|
-
event === "video_uploaded" ||
|
|
194
|
-
event === "change_user_devices" ||
|
|
195
|
-
event === "queue" ||
|
|
196
|
-
event === "title_changed"
|
|
197
|
-
) {
|
|
198
|
-
this.emit('message', {event, ...payload, roomId})
|
|
199
|
-
}
|
|
200
|
-
else if(event === 'joined' || event === 'leaving') {
|
|
201
|
-
this.emit('message', {event, ...payload, isObserver:!!payload.isObserver, roomId});
|
|
202
|
-
}
|
|
203
|
-
else if(
|
|
204
|
-
event === 'left' || //user removed room a.k.a. left the room
|
|
205
|
-
event === 'kicked' ||
|
|
206
|
-
event === 'banned' ||
|
|
207
|
-
event === 'unbanned' ||
|
|
208
|
-
event === 'approved' ||
|
|
209
|
-
event === 'muted' ||
|
|
210
|
-
event === 'unmuted' ||
|
|
211
|
-
event === 'messageRemoved' ||
|
|
212
|
-
event === 'messageReported' ||
|
|
213
|
-
event === 'chatClear' ||
|
|
214
|
-
event === 'handRaised' || event === 'handLowered' || event === 'handsCleared'
|
|
215
|
-
) {
|
|
216
|
-
this.emit('message', {event, ...payload});
|
|
217
|
-
}
|
|
218
|
-
else if(event === 'volume_set') {
|
|
219
|
-
this.emit('message', {event, ...payload});
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
else if(topic[1] === 'instanceroom') { // instance
|
|
223
|
-
if(event === 'add_room' || event === 'remove_room' || event === 'set_room' || event === "instance_homepage_changed" || event === 'instance_settings_changed') {
|
|
224
|
-
this.emit('message', {event, ...payload});
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
else if(topic[1] === 'externalmix') {
|
|
228
|
-
const event = payload.event;
|
|
229
|
-
this.emit('message', {event, ...payload});
|
|
230
|
-
}
|
|
231
|
-
} else if(topic[0] === 'wtr') {
|
|
232
|
-
const recorderId = topic[1];
|
|
233
|
-
const sessionId = topic[2];
|
|
234
|
-
if(topic[3] === 'control') {
|
|
235
|
-
this.emit('message', {event: 'recorder_control', ...payload, recorderId, sessionId});
|
|
236
|
-
} // recorder control
|
|
237
|
-
else if(topic[3] === 'monitor') {
|
|
238
|
-
this.emit('message', {event: 'recorder_monitor', ...payload, recorderId, sessionId});
|
|
239
|
-
} // recorder monitor
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
export default Iot;
|