@realsee/live 0.5.0-alpha.5 → 0.5.0-alpha.8

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.
@@ -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() {
@@ -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
  * 解除网络监听事件
@@ -1332,6 +1385,7 @@ class RobustWebSocket extends Subscribe {
1332
1385
  const attempts = this._attempts;
1333
1386
  this._attempts = 0;
1334
1387
  this._reconnectWhenOnlineAgain = false;
1388
+ this._startHealthCheck();
1335
1389
  return this.emit('open', Object.assign(event, { detail: { reconnects, attempts } }));
1336
1390
  };
1337
1391
  /**
@@ -1341,6 +1395,7 @@ class RobustWebSocket extends Subscribe {
1341
1395
  */
1342
1396
  this._webSocketCloseHandler = (event) => {
1343
1397
  this._reconnect(event);
1398
+ this._stopHealthCheck();
1344
1399
  return this.emit('close', event);
1345
1400
  };
1346
1401
  /**
@@ -1362,6 +1417,7 @@ class RobustWebSocket extends Subscribe {
1362
1417
  if (options) {
1363
1418
  this.setOptions(options);
1364
1419
  }
1420
+ this._startHealthCheck();
1365
1421
  }
1366
1422
  get attempts() {
1367
1423
  return this._attempts;
@@ -1412,7 +1468,10 @@ class LiveController extends Subscribe {
1412
1468
  /**
1413
1469
  * ws代理器
1414
1470
  */
1415
- this._ws = new RobustWebSocket();
1471
+ this._ws = new RobustWebSocket({
1472
+ url: '',
1473
+ robustOptions: { timeout: 180000 },
1474
+ });
1416
1475
  /**
1417
1476
  * 带看连接状态
1418
1477
  */
@@ -1422,6 +1481,7 @@ class LiveController extends Subscribe {
1422
1481
  */
1423
1482
  this._addWebSocketEventListeners = () => {
1424
1483
  this._ws.on('close', this._handleCloseEvent);
1484
+ this._ws.on('closing', this._handleClosingEvent);
1425
1485
  this._ws.on('connecting', this._handleConnectingEvent);
1426
1486
  this._ws.on('error', this._handleErrorEvent);
1427
1487
  this._ws.on('message', this._handleMessageEvent);
@@ -1433,6 +1493,7 @@ class LiveController extends Subscribe {
1433
1493
  */
1434
1494
  this._removeWebSocketEventListeners = () => {
1435
1495
  this._ws.off('close', this._handleCloseEvent);
1496
+ this._ws.off('closing', this._handleClosingEvent);
1436
1497
  this._ws.off('connecting', this._handleConnectingEvent);
1437
1498
  this._ws.off('error', this._handleErrorEvent);
1438
1499
  this._ws.off('message', this._handleMessageEvent);
@@ -1447,12 +1508,20 @@ class LiveController extends Subscribe {
1447
1508
  logger$1.info(`live room closed bacause of ${event.code}-${event.reason || parseWSClsErr(event.code)}`);
1448
1509
  this.state = LiveState.CLOSED;
1449
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
+ };
1450
1519
  /**
1451
1520
  * 连接中
1452
1521
  * @param event
1453
1522
  */
1454
- this._handleConnectingEvent = (event) => {
1455
- logger$1.info(`live room is connecting`, event.detail);
1523
+ this._handleConnectingEvent = () => {
1524
+ logger$1.info(`live room is connecting`);
1456
1525
  this.state = LiveState.CONNECTING;
1457
1526
  };
1458
1527
  /**
@@ -1615,7 +1684,6 @@ class Live extends Subscribe {
1615
1684
  this._addLiveControllerEventListeners = () => {
1616
1685
  this._controller.on('stateChange', this._handleStateChange);
1617
1686
  this._controller.on('message', this._handleMessage);
1618
- this._controller.on('reconnected', this._handleReconnected);
1619
1687
  };
1620
1688
  /**
1621
1689
  * 移除带看控制的事件监听
@@ -1623,7 +1691,6 @@ class Live extends Subscribe {
1623
1691
  this._removeLiveControllerEventListeners = () => {
1624
1692
  this._controller.off('stateChange', this._handleStateChange);
1625
1693
  this._controller.off('message', this._handleMessage);
1626
- this._controller.off('reconnected', this._handleReconnected);
1627
1694
  };
1628
1695
  /**
1629
1696
  * 带看状态变化
@@ -1778,32 +1845,6 @@ class Live extends Subscribe {
1778
1845
  }
1779
1846
  }
1780
1847
  });
1781
- /**
1782
- * 处理重连
1783
- * @param detail
1784
- */
1785
- this._handleReconnected = (detail) => __awaiter(this, void 0, void 0, function* () {
1786
- var _h, _j, _k, _l, _m, _o, _p;
1787
- // 已经重连上ws,尝试重连语音
1788
- this.$RTC.joined = false;
1789
- const { rtcInfo } = this._buildInData;
1790
- if (rtcInfo) {
1791
- const { voiceId, type } = rtcInfo;
1792
- const result = yield ((_h = this.$RTC) === null || _h === void 0 ? void 0 : _h.join({
1793
- voiceId,
1794
- userId: ((_j = this.selfInfo) === null || _j === void 0 ? void 0 : _j.id) || '',
1795
- roomId: (_k = this.roomInfo) === null || _k === void 0 ? void 0 : _k.info.room_code,
1796
- type,
1797
- }));
1798
- if (result) {
1799
- const status = (_o = (_m = (_l = this.selfInfo) === null || _l === void 0 ? void 0 : _l.micro_status) !== null && _m !== void 0 ? _m : this._options.initialMicroStatus) !== null && _o !== void 0 ? _o : MicroStatus.OPEN;
1800
- yield ((_p = this.$RTC) === null || _p === void 0 ? void 0 : _p.toggleMicro(status === MicroStatus.OPEN));
1801
- }
1802
- else {
1803
- logger$1.error('加入语音房间失败');
1804
- }
1805
- }
1806
- });
1807
1848
  /**
1808
1849
  * 清理带看缓存的数据
1809
1850
  */
@@ -1816,12 +1857,12 @@ class Live extends Subscribe {
1816
1857
  * @returns
1817
1858
  */
1818
1859
  this.connect = (options = {}) => __awaiter(this, void 0, void 0, function* () {
1819
- var _q, _r, _s, _t;
1860
+ var _h, _j, _k, _l;
1820
1861
  // 检查 url 和 票据获取接口实现
1821
- this._options.url = (_r = (_q = options.url) !== null && _q !== void 0 ? _q : this._options.url) !== null && _r !== void 0 ? _r : '';
1822
- this._options.getTicket = (_s = options.getTicket) !== null && _s !== void 0 ? _s : this._options.getTicket;
1862
+ this._options.url = (_j = (_h = options.url) !== null && _h !== void 0 ? _h : this._options.url) !== null && _j !== void 0 ? _j : '';
1863
+ this._options.getTicket = (_k = options.getTicket) !== null && _k !== void 0 ? _k : this._options.getTicket;
1823
1864
  this._options.initialMicroStatus =
1824
- (_t = options.rtcMicStatus) !== null && _t !== void 0 ? _t : this._options.initialMicroStatus;
1865
+ (_l = options.rtcMicStatus) !== null && _l !== void 0 ? _l : this._options.initialMicroStatus;
1825
1866
  if (!/^ws(s):\/\//.test(this._options.url) ||
1826
1867
  typeof this._options.getTicket !== 'function') {
1827
1868
  if (!/^ws(s):\/\//.test(this._options.url)) {
@@ -1863,11 +1904,11 @@ class Live extends Subscribe {
1863
1904
  * @returns
1864
1905
  */
1865
1906
  this.exit = () => __awaiter(this, void 0, void 0, function* () {
1866
- var _u;
1907
+ var _m;
1867
1908
  this._clearLiveData();
1868
1909
  try {
1869
1910
  /** 退出强制关闭语音 */
1870
- (_u = this.$RTC) === null || _u === void 0 ? void 0 : _u.quit();
1911
+ (_m = this.$RTC) === null || _m === void 0 ? void 0 : _m.quit();
1871
1912
  yield this.updateStatus(BuiltinEventType.RoomChange, BuiltinActionType.RoomClose, {});
1872
1913
  }
1873
1914
  catch (e) {
@@ -1894,10 +1935,10 @@ class Live extends Subscribe {
1894
1935
  });
1895
1936
  // 仅用于用户自己的麦克风控制
1896
1937
  this.toggleMicro = (microStatus) => __awaiter(this, void 0, void 0, function* () {
1897
- var _v, _w, _x;
1898
- if (!((_w = (_v = this.selfInfo) === null || _v === void 0 ? void 0 : _v.permission) === null || _w === void 0 ? void 0 : _w.control_own_micro))
1938
+ var _o, _p, _q;
1939
+ if (!((_p = (_o = this.selfInfo) === null || _o === void 0 ? void 0 : _o.permission) === null || _p === void 0 ? void 0 : _p.control_own_micro))
1899
1940
  return false;
1900
- const result = yield ((_x = this.$RTC) === null || _x === void 0 ? void 0 : _x.toggleMicro(microStatus));
1941
+ const result = yield ((_q = this.$RTC) === null || _q === void 0 ? void 0 : _q.toggleMicro(microStatus));
1901
1942
  if (result) {
1902
1943
  yield this.updateStatus(BuiltinEventType.UserChange, BuiltinActionType.UserMicroStatus, {
1903
1944
  status: microStatus ? MicroStatus.OPEN : MicroStatus.CLOSE,
@@ -1912,12 +1953,12 @@ class Live extends Subscribe {
1912
1953
  * @returns
1913
1954
  */
1914
1955
  this.forbidMicro = (microStatus, toUserIds) => __awaiter(this, void 0, void 0, function* () {
1915
- var _y, _z;
1956
+ var _r, _s;
1916
1957
  if (typeof this._options.canForbidUserMicro === 'function' &&
1917
1958
  !this._options.canForbidUserMicro(toUserIds)) {
1918
1959
  return false;
1919
1960
  }
1920
- if (!((_z = (_y = this.selfInfo) === null || _y === void 0 ? void 0 : _y.permission) === null || _z === void 0 ? void 0 : _z.control_other_micro)) {
1961
+ if (!((_s = (_r = this.selfInfo) === null || _r === void 0 ? void 0 : _r.permission) === null || _s === void 0 ? void 0 : _s.control_other_micro)) {
1921
1962
  return false;
1922
1963
  }
1923
1964
  return yield this.updateStatus(BuiltinEventType.UserChange, BuiltinActionType.UserMicroStatus, {
@@ -1931,12 +1972,12 @@ class Live extends Subscribe {
1931
1972
  * @returns
1932
1973
  */
1933
1974
  this.forbidSync = (sync, toUserIds) => __awaiter(this, void 0, void 0, function* () {
1934
- var _0, _1;
1975
+ var _t, _u;
1935
1976
  if (typeof this._options.canForbidUserSync === 'function' &&
1936
1977
  !this._options.canForbidUserSync(toUserIds)) {
1937
1978
  return false;
1938
1979
  }
1939
- if (!((_1 = (_0 = this.selfInfo) === null || _0 === void 0 ? void 0 : _0.permission) === null || _1 === void 0 ? void 0 : _1.control_other_sync)) {
1980
+ if (!((_u = (_t = this.selfInfo) === null || _t === void 0 ? void 0 : _t.permission) === null || _u === void 0 ? void 0 : _u.control_other_sync)) {
1940
1981
  return false;
1941
1982
  }
1942
1983
  return yield this.updateStatus(BuiltinEventType.UserChange, BuiltinActionType.UserPermission, { sync_frame: sync }, toUserIds);
@@ -1947,12 +1988,12 @@ class Live extends Subscribe {
1947
1988
  * @returns
1948
1989
  */
1949
1990
  this.kick = (toUserIds) => __awaiter(this, void 0, void 0, function* () {
1950
- var _2, _3;
1991
+ var _v, _w;
1951
1992
  if (typeof this._options.canForbidUserKick === 'function' &&
1952
1993
  !this._options.canForbidUserKick(toUserIds)) {
1953
1994
  return false;
1954
1995
  }
1955
- if (!((_3 = (_2 = this.selfInfo) === null || _2 === void 0 ? void 0 : _2.permission) === null || _3 === void 0 ? void 0 : _3.change_other_status)) {
1996
+ if (!((_w = (_v = this.selfInfo) === null || _v === void 0 ? void 0 : _v.permission) === null || _w === void 0 ? void 0 : _w.change_other_status)) {
1956
1997
  return false;
1957
1998
  }
1958
1999
  return yield this.updateStatus(BuiltinEventType.UserChange, BuiltinActionType.UserStatus, {