@realsee/live 0.5.0-alpha.1 → 0.5.0-alpha.10

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.
@@ -1,7 +1,7 @@
1
1
  import JSBridge from '@realsee/jsbridge-x';
2
2
  import TRTC from 'trtc-js-sdk';
3
3
  import { DefaultModality, ConsoleLogger, LogLevel as LogLevel$1, DefaultDeviceController, MeetingSessionConfiguration, DefaultMeetingSession, MeetingSessionStatusCode } from 'amazon-chime-sdk-js';
4
- import React, { createContext, useCallback } from 'react';
4
+ import React, { createContext } from 'react';
5
5
 
6
6
  const EVENT_SYMBOL = Symbol('$$REALSEE_LIVE_EVENT$$');
7
7
  function __generateEventIfNotExisted(instance) {
@@ -608,9 +608,12 @@ class VRWebViewRTC extends CoreRTC {
608
608
  this.micro = false;
609
609
  this.voiceId = '';
610
610
  this.options = { jsBridge, getVoiceSign };
611
- this.jsBridge.callAndListen('live/userVolumes', (userVolumes) => {
612
- this.emit('userVolumes', userVolumes);
613
- });
611
+ // TO-DO: 在微信小程序下有个音量上报的BUG会导致消息体过大从而jsbridge异常断开
612
+ if (this.jsBridge.webViewType !== 'WxMiniProgramWebView') {
613
+ this.jsBridge.callAndListen('live/userVolumes', (userVolumes) => {
614
+ this.emit('userVolumes', userVolumes);
615
+ });
616
+ }
614
617
  this.jsBridge.callAndListen('live/weakNetwork', (weakNetwork) => {
615
618
  if (weakNetwork)
616
619
  this.emit('weakNetwork');
@@ -621,25 +624,37 @@ class VRWebViewRTC extends CoreRTC {
621
624
  }
622
625
  join(options) {
623
626
  return __awaiter(this, void 0, void 0, function* () {
624
- this.emit('joinWillStart');
625
- const { sdkAppId, userId, userSig } = yield this.options.getVoiceSign(options);
626
- this.type = options.type;
627
- const [res, error] = yield this.jsBridge.callAndBackfeed(`live/join?sdkAppID=${sdkAppId}&userID=${userId}&roomID=${Number(options.voiceId)}&userSig=${userSig}`);
628
- if (!res) {
629
- this.emit('error', error ? error : new Error('加入房间失败:未知异常。'));
630
- this.joined = false;
631
- }
632
- else {
633
- if (res.code === RTCJoinedInfoCodeEnum.Success) {
634
- this.emit('joined');
635
- this.joined = true;
627
+ try {
628
+ this.emit('joinWillStart');
629
+ const { sdkAppId, userId, userSig } = yield this.options.getVoiceSign(options);
630
+ if (!(Boolean(sdkAppId) && Boolean(userId) && Boolean(userSig))) {
631
+ this.emit('error', new Error('加入房间失败:rtc 签名请求异常'));
632
+ this.joined = false;
633
+ return this.joined;
636
634
  }
637
- else {
638
- this.emit('error', res);
635
+ this.type = options.type;
636
+ const [res, error] = yield this.jsBridge.callAndBackfeed(`live/join?sdkAppID=${sdkAppId}&userID=${userId}&roomID=${Number(options.voiceId)}&userSig=${userSig}`);
637
+ if (!res) {
638
+ this.emit('error', error ? error : new Error('加入房间失败:未知异常。'));
639
639
  this.joined = false;
640
640
  }
641
+ else {
642
+ if (res.code === RTCJoinedInfoCodeEnum.Success) {
643
+ this.emit('joined');
644
+ this.joined = true;
645
+ }
646
+ else {
647
+ this.emit('error', res);
648
+ this.joined = false;
649
+ }
650
+ }
651
+ return this.joined;
652
+ }
653
+ catch (e) {
654
+ this.emit('error', new Error('加入房间失败:' + e.message));
655
+ this.joined = false;
656
+ return this.joined;
641
657
  }
642
- return this.joined;
643
658
  });
644
659
  }
645
660
  quit() {
@@ -785,6 +800,23 @@ function parseLiveMsg(jsonTxt) {
785
800
  }
786
801
  }
787
802
 
803
+ var LiveState;
804
+ (function (LiveState) {
805
+ LiveState[LiveState["NOTINITIALIZED"] = -1] = "NOTINITIALIZED";
806
+ /**
807
+ * 正在链接中
808
+ */
809
+ LiveState[LiveState["CONNECTING"] = 0] = "CONNECTING";
810
+ /**
811
+ * 已经链接并且可以通讯
812
+ */
813
+ LiveState[LiveState["OPEN"] = 1] = "OPEN";
814
+ /**
815
+ * 连接已关闭或者链接失败
816
+ */
817
+ LiveState[LiveState["CLOSED"] = 3] = "CLOSED";
818
+ })(LiveState || (LiveState = {}));
819
+
788
820
  /**
789
821
  * 用户当前状态枚举。
790
822
  */
@@ -942,23 +974,6 @@ exports.default = LogLevel;
942
974
 
943
975
  var LogLevel = /*@__PURE__*/getDefaultExportFromCjs(LogLevel_1);
944
976
 
945
- var LiveState;
946
- (function (LiveState) {
947
- LiveState[LiveState["NOTINITIALIZED"] = -1] = "NOTINITIALIZED";
948
- /**
949
- * 正在链接中
950
- */
951
- LiveState[LiveState["CONNECTING"] = 0] = "CONNECTING";
952
- /**
953
- * 已经链接并且可以通讯
954
- */
955
- LiveState[LiveState["OPEN"] = 1] = "OPEN";
956
- /**
957
- * 连接已关闭或者链接失败
958
- */
959
- LiveState[LiveState["CLOSED"] = 3] = "CLOSED";
960
- })(LiveState || (LiveState = {}));
961
-
962
977
  function appendUri(uri, query) {
963
978
  const hasQuery = uri.indexOf('?') !== -1;
964
979
  const newQuery = Object.keys(query).reduce((accu, curr, idx) => `${accu}${(idx === 0 && !hasQuery) ? '?' : '&'}${curr}=${query[curr]}`, '');
@@ -1160,6 +1175,7 @@ class RobustWebSocket extends Subscribe {
1160
1175
  constructor(options) {
1161
1176
  // eslint-disable-next-line @typescript-eslint/no-unsafe-call
1162
1177
  super();
1178
+ this._readyState = WebSocketState.NOTINITIALIZED;
1163
1179
  this._options = {
1164
1180
  timeout: 10000,
1165
1181
  shouldReconnect: (event, ws) => {
@@ -1176,6 +1192,43 @@ class RobustWebSocket extends Subscribe {
1176
1192
  this._reconnectWhenOnlineAgain = false;
1177
1193
  this._explicitlyClosed = false;
1178
1194
  this._connectivityEventsAttached = false;
1195
+ /**
1196
+ * 做一个健康检查
1197
+ */
1198
+ this._healthCheck = () => __awaiter(this, void 0, void 0, function* () {
1199
+ if (this._realWebSocket &&
1200
+ this._realWebSocket.readyState !== this._readyState) {
1201
+ this._readyState = this._realWebSocket.readyState;
1202
+ // 太惨了,断网的时候,ws不会直接closed,而是处于closing的状态
1203
+ if (this._readyState === WebSocketState.CLOSING) {
1204
+ this.emit('closing');
1205
+ }
1206
+ }
1207
+ yield new Promise((resolve) => setTimeout(resolve, 2000));
1208
+ this._heathCheckTimer = requestAnimationFrame(this._healthCheck);
1209
+ });
1210
+ /**
1211
+ * 清理健康检查
1212
+ */
1213
+ this._clearHeathCheckIfNeed = () => {
1214
+ if (this._heathCheckTimer) {
1215
+ cancelAnimationFrame(this._heathCheckTimer);
1216
+ this._heathCheckTimer = undefined;
1217
+ }
1218
+ };
1219
+ /**
1220
+ * 开启健康检查
1221
+ */
1222
+ this._startHealthCheck = () => {
1223
+ this._clearHeathCheckIfNeed();
1224
+ this._heathCheckTimer = requestAnimationFrame(this._healthCheck);
1225
+ };
1226
+ /**
1227
+ * 停止健康检查
1228
+ */
1229
+ this._stopHealthCheck = () => {
1230
+ this._clearHeathCheckIfNeed();
1231
+ };
1179
1232
  /**
1180
1233
  * 清理连接中的请求
1181
1234
  */
@@ -1201,7 +1254,7 @@ class RobustWebSocket extends Subscribe {
1201
1254
  this._offline = () => {
1202
1255
  var _a;
1203
1256
  this._reconnectWhenOnlineAgain = true;
1204
- (_a = this._realWebSocket) === null || _a === void 0 ? void 0 : _a.close(1000);
1257
+ (_a = this._realWebSocket) === null || _a === void 0 ? void 0 : _a.close(1000, 'offline');
1205
1258
  };
1206
1259
  /**
1207
1260
  * 解除网络监听事件
@@ -1228,7 +1281,12 @@ class RobustWebSocket extends Subscribe {
1228
1281
  * @param data
1229
1282
  * @returns
1230
1283
  */
1231
- this.send = (data) => { var _a; return (_a = this._realWebSocket) === null || _a === void 0 ? void 0 : _a.send(data); };
1284
+ this.send = (data) => {
1285
+ var _a, _b, _c;
1286
+ if (((_a = this._realWebSocket) === null || _a === void 0 ? void 0 : _a.readyState) === ((_b = this._realWebSocket) === null || _b === void 0 ? void 0 : _b.OPEN)) {
1287
+ (_c = this._realWebSocket) === null || _c === void 0 ? void 0 : _c.send(data);
1288
+ }
1289
+ };
1232
1290
  /**
1233
1291
  * 关闭连接
1234
1292
  * @param code 代码
@@ -1272,6 +1330,7 @@ class RobustWebSocket extends Subscribe {
1272
1330
  if (!newUrl)
1273
1331
  return;
1274
1332
  this._pendingReconnect = undefined;
1333
+ this._removeWebSocketEvent();
1275
1334
  this._realWebSocket = new WebSocket(newUrl, this._protocols || undefined);
1276
1335
  this._attempts = this._attempts + 1;
1277
1336
  this.emit('connecting', new CustomEvent('connecting', {
@@ -1293,6 +1352,16 @@ class RobustWebSocket extends Subscribe {
1293
1352
  this._attachConnectivityEvents();
1294
1353
  }
1295
1354
  };
1355
+ /**
1356
+ * 断开ws事件
1357
+ */
1358
+ this._removeWebSocketEvent = () => {
1359
+ var _a, _b, _c, _d;
1360
+ (_a = this._realWebSocket) === null || _a === void 0 ? void 0 : _a.removeEventListener('open', this._webSocketOpenHandler);
1361
+ (_b = this._realWebSocket) === null || _b === void 0 ? void 0 : _b.removeEventListener('close', this._webSocketCloseHandler);
1362
+ (_c = this._realWebSocket) === null || _c === void 0 ? void 0 : _c.removeEventListener('error', this._webSocketErrorHandler);
1363
+ (_d = this._realWebSocket) === null || _d === void 0 ? void 0 : _d.removeEventListener('message', this._webSocketMessageHandler);
1364
+ };
1296
1365
  /**
1297
1366
  * 分发ws事件
1298
1367
  */
@@ -1316,6 +1385,7 @@ class RobustWebSocket extends Subscribe {
1316
1385
  const attempts = this._attempts;
1317
1386
  this._attempts = 0;
1318
1387
  this._reconnectWhenOnlineAgain = false;
1388
+ this._startHealthCheck();
1319
1389
  return this.emit('open', Object.assign(event, { detail: { reconnects, attempts } }));
1320
1390
  };
1321
1391
  /**
@@ -1325,6 +1395,7 @@ class RobustWebSocket extends Subscribe {
1325
1395
  */
1326
1396
  this._webSocketCloseHandler = (event) => {
1327
1397
  this._reconnect(event);
1398
+ this._stopHealthCheck();
1328
1399
  return this.emit('close', event);
1329
1400
  };
1330
1401
  /**
@@ -1346,6 +1417,7 @@ class RobustWebSocket extends Subscribe {
1346
1417
  if (options) {
1347
1418
  this.setOptions(options);
1348
1419
  }
1420
+ this._startHealthCheck();
1349
1421
  }
1350
1422
  get attempts() {
1351
1423
  return this._attempts;
@@ -1396,7 +1468,10 @@ class LiveController extends Subscribe {
1396
1468
  /**
1397
1469
  * ws代理器
1398
1470
  */
1399
- this._ws = new RobustWebSocket();
1471
+ this._ws = new RobustWebSocket({
1472
+ url: '',
1473
+ robustOptions: { timeout: 180000 },
1474
+ });
1400
1475
  /**
1401
1476
  * 带看连接状态
1402
1477
  */
@@ -1406,6 +1481,7 @@ class LiveController extends Subscribe {
1406
1481
  */
1407
1482
  this._addWebSocketEventListeners = () => {
1408
1483
  this._ws.on('close', this._handleCloseEvent);
1484
+ this._ws.on('closing', this._handleClosingEvent);
1409
1485
  this._ws.on('connecting', this._handleConnectingEvent);
1410
1486
  this._ws.on('error', this._handleErrorEvent);
1411
1487
  this._ws.on('message', this._handleMessageEvent);
@@ -1417,6 +1493,7 @@ class LiveController extends Subscribe {
1417
1493
  */
1418
1494
  this._removeWebSocketEventListeners = () => {
1419
1495
  this._ws.off('close', this._handleCloseEvent);
1496
+ this._ws.off('closing', this._handleClosingEvent);
1420
1497
  this._ws.off('connecting', this._handleConnectingEvent);
1421
1498
  this._ws.off('error', this._handleErrorEvent);
1422
1499
  this._ws.off('message', this._handleMessageEvent);
@@ -1431,12 +1508,20 @@ class LiveController extends Subscribe {
1431
1508
  logger$1.info(`live room closed bacause of ${event.code}-${event.reason || parseWSClsErr(event.code)}`);
1432
1509
  this.state = LiveState.CLOSED;
1433
1510
  };
1511
+ /**
1512
+ * 处理房间正在关闭事件(这个时候可能是断网了)
1513
+ */
1514
+ this._handleClosingEvent = () => {
1515
+ var _a;
1516
+ logger$1.info(`live room is closing, onLine:`, (_a = globalThis === null || globalThis === void 0 ? void 0 : globalThis.navigator) === null || _a === void 0 ? void 0 : _a.onLine);
1517
+ this.state = LiveState.CLOSED;
1518
+ };
1434
1519
  /**
1435
1520
  * 连接中
1436
1521
  * @param event
1437
1522
  */
1438
- this._handleConnectingEvent = (event) => {
1439
- logger$1.info(`live room is connecting`, event.detail);
1523
+ this._handleConnectingEvent = () => {
1524
+ logger$1.info(`live room is connecting`);
1440
1525
  this.state = LiveState.CONNECTING;
1441
1526
  };
1442
1527
  /**
@@ -1470,6 +1555,9 @@ class LiveController extends Subscribe {
1470
1555
  this._handleOpenEvent = (event) => {
1471
1556
  logger$1.info('live room open', event.detail);
1472
1557
  this.state = LiveState.OPEN;
1558
+ if (event.detail.reconnects > 0) {
1559
+ this.emit('reconnected', event.detail);
1560
+ }
1473
1561
  };
1474
1562
  /**
1475
1563
  * 处理超时
@@ -1610,6 +1698,10 @@ class Live extends Subscribe {
1610
1698
  * @returns
1611
1699
  */
1612
1700
  this._handleStateChange = (state, prevState) => this.emit('stateChange', state, prevState);
1701
+ /**
1702
+ * 消息接收
1703
+ * @param liveMsg
1704
+ */
1613
1705
  this._handleMessage = (liveMsg) => __awaiter(this, void 0, void 0, function* () {
1614
1706
  var _b, _c, _d, _e, _f, _g;
1615
1707
  if (typeof liveMsg === 'object') {
@@ -1641,6 +1733,7 @@ class Live extends Subscribe {
1641
1733
  */
1642
1734
  case Command.RTCInfo: {
1643
1735
  const { voice_id: voiceId, type } = data;
1736
+ this._buildInData.rtcInfo = { voiceId, type };
1644
1737
  if (!((_b = this.$RTC) === null || _b === void 0 ? void 0 : _b.joined)) {
1645
1738
  const result = yield ((_c = this.$RTC) === null || _c === void 0 ? void 0 : _c.join({
1646
1739
  voiceId,
@@ -1781,6 +1874,13 @@ class Live extends Subscribe {
1781
1874
  return false;
1782
1875
  }
1783
1876
  const { force = false } = options;
1877
+ // 检测一下麦克风权限,hack
1878
+ if (this._options.rtc && this._options.jsBridge) {
1879
+ const res = yield this._options.jsBridge.detectMicro();
1880
+ if (!res) {
1881
+ return new Error('Failed to obtain microphone authorization');
1882
+ }
1883
+ }
1784
1884
  // 检查是否已有实例在使用
1785
1885
  const currentState = this.state;
1786
1886
  if (currentState === LiveState.CONNECTING ||
@@ -2155,21 +2255,21 @@ function createLiveReact(options) {
2155
2255
  * @deprecated
2156
2256
  * @returns
2157
2257
  */
2158
- const unsafe__useLiveInstance = useCallback(() => useLiveInstance(), [useLiveInstance]);
2258
+ const unsafe__useLiveInstance = () => useLiveInstance();
2159
2259
  /**
2160
2260
  * 使用rtc实例
2161
2261
  */
2162
- const useRTCInstance = useCallback(() => useLiveInstance().$RTC, [useLiveInstance]);
2262
+ const useRTCInstance = () => useLiveInstance().$RTC;
2163
2263
  /**
2164
2264
  * 不建议这样使用
2165
2265
  * @deprecated
2166
2266
  * @returns
2167
2267
  */
2168
- const unsafe__useRTCInstance = useCallback(() => useRTCInstance(), [useRTCInstance]);
2268
+ const unsafe__useRTCInstance = () => useRTCInstance();
2169
2269
  /**
2170
2270
  * 带看连接操作
2171
2271
  */
2172
- const useConnect = useCallback((options) => __awaiter(this, void 0, void 0, function* () { return yield useLiveInstance().connect(options); }), []);
2272
+ const useConnect = (options) => __awaiter(this, void 0, void 0, function* () { return yield useLiveInstance().connect(options); });
2173
2273
  /**
2174
2274
  *
2175
2275
  * @param name
@@ -2333,5 +2433,5 @@ function createLiveReact(options) {
2333
2433
  };
2334
2434
  }
2335
2435
 
2336
- export { BrowserRTC, BrowserRTC4Chime, BuiltInRTCType, BuiltinActionType, BuiltinEventType, Command, CoreRTC, MemUserStatus, MicroStatus, RTCJoinedInfoCodeEnum, RTCType, UserStatus, VRWebViewRTC, WebSocketCloseError, WebSocketErrorType, WebSocketState, createLive, createLiveReact, parseLiveMsg, parseWSClsErr };
2436
+ export { BrowserRTC, BrowserRTC4Chime, BuiltInRTCType, BuiltinActionType, BuiltinEventType, Command, CoreRTC, LiveState, MemUserStatus, MicroStatus, RTCJoinedInfoCodeEnum, RTCType, UserStatus, VRWebViewRTC, WebSocketCloseError, WebSocketErrorType, WebSocketState, createLive, createLiveReact, parseLiveMsg, parseWSClsErr };
2337
2437
  //# sourceMappingURL=realsee-live.esm.js.map