@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.
@@ -32,6 +32,10 @@ export declare class LiveController extends Subscribe<LiveControllerEventMap> {
32
32
  * @param event
33
33
  */
34
34
  private _handleCloseEvent;
35
+ /**
36
+ * 处理房间正在关闭事件(这个时候可能是断网了)
37
+ */
38
+ private _handleClosingEvent;
35
39
  /**
36
40
  * 连接中
37
41
  * @param event
@@ -103,11 +103,6 @@ export declare class Live<KeyframesSnapshot extends Record<string, any>> extends
103
103
  * @param liveMsg
104
104
  */
105
105
  private _handleMessage;
106
- /**
107
- * 处理重连
108
- * @param detail
109
- */
110
- private _handleReconnected;
111
106
  /**
112
107
  * 清理带看缓存的数据
113
108
  */
@@ -618,9 +618,12 @@ class VRWebViewRTC extends CoreRTC {
618
618
  this.micro = false;
619
619
  this.voiceId = '';
620
620
  this.options = { jsBridge, getVoiceSign };
621
- this.jsBridge.callAndListen('live/userVolumes', (userVolumes) => {
622
- this.emit('userVolumes', userVolumes);
623
- });
621
+ // TO-DO: 在微信小程序下有个音量上报的BUG会导致消息体过大从而jsbridge异常断开
622
+ if (this.jsBridge.webViewType !== 'WxMiniProgramWebView') {
623
+ this.jsBridge.callAndListen('live/userVolumes', (userVolumes) => {
624
+ this.emit('userVolumes', userVolumes);
625
+ });
626
+ }
624
627
  this.jsBridge.callAndListen('live/weakNetwork', (weakNetwork) => {
625
628
  if (weakNetwork)
626
629
  this.emit('weakNetwork');
@@ -631,25 +634,37 @@ class VRWebViewRTC extends CoreRTC {
631
634
  }
632
635
  join(options) {
633
636
  return __awaiter(this, void 0, void 0, function* () {
634
- this.emit('joinWillStart');
635
- const { sdkAppId, userId, userSig } = yield this.options.getVoiceSign(options);
636
- this.type = options.type;
637
- const [res, error] = yield this.jsBridge.callAndBackfeed(`live/join?sdkAppID=${sdkAppId}&userID=${userId}&roomID=${Number(options.voiceId)}&userSig=${userSig}`);
638
- if (!res) {
639
- this.emit('error', error ? error : new Error('加入房间失败:未知异常。'));
640
- this.joined = false;
641
- }
642
- else {
643
- if (res.code === exports.RTCJoinedInfoCodeEnum.Success) {
644
- this.emit('joined');
645
- this.joined = true;
637
+ try {
638
+ this.emit('joinWillStart');
639
+ const { sdkAppId, userId, userSig } = yield this.options.getVoiceSign(options);
640
+ if (!(Boolean(sdkAppId) && Boolean(userId) && Boolean(userSig))) {
641
+ this.emit('error', new Error('加入房间失败:rtc 签名请求异常'));
642
+ this.joined = false;
643
+ return this.joined;
646
644
  }
647
- else {
648
- this.emit('error', res);
645
+ this.type = options.type;
646
+ const [res, error] = yield this.jsBridge.callAndBackfeed(`live/join?sdkAppID=${sdkAppId}&userID=${userId}&roomID=${Number(options.voiceId)}&userSig=${userSig}`);
647
+ if (!res) {
648
+ this.emit('error', error ? error : new Error('加入房间失败:未知异常。'));
649
649
  this.joined = false;
650
650
  }
651
+ else {
652
+ if (res.code === exports.RTCJoinedInfoCodeEnum.Success) {
653
+ this.emit('joined');
654
+ this.joined = true;
655
+ }
656
+ else {
657
+ this.emit('error', res);
658
+ this.joined = false;
659
+ }
660
+ }
661
+ return this.joined;
662
+ }
663
+ catch (e) {
664
+ this.emit('error', new Error('加入房间失败:' + e.message));
665
+ this.joined = false;
666
+ return this.joined;
651
667
  }
652
- return this.joined;
653
668
  });
654
669
  }
655
670
  quit() {
@@ -1170,6 +1185,7 @@ class RobustWebSocket extends Subscribe {
1170
1185
  constructor(options) {
1171
1186
  // eslint-disable-next-line @typescript-eslint/no-unsafe-call
1172
1187
  super();
1188
+ this._readyState = exports.WebSocketState.NOTINITIALIZED;
1173
1189
  this._options = {
1174
1190
  timeout: 10000,
1175
1191
  shouldReconnect: (event, ws) => {
@@ -1186,6 +1202,43 @@ class RobustWebSocket extends Subscribe {
1186
1202
  this._reconnectWhenOnlineAgain = false;
1187
1203
  this._explicitlyClosed = false;
1188
1204
  this._connectivityEventsAttached = false;
1205
+ /**
1206
+ * 做一个健康检查
1207
+ */
1208
+ this._healthCheck = () => __awaiter(this, void 0, void 0, function* () {
1209
+ if (this._realWebSocket &&
1210
+ this._realWebSocket.readyState !== this._readyState) {
1211
+ this._readyState = this._realWebSocket.readyState;
1212
+ // 太惨了,断网的时候,ws不会直接closed,而是处于closing的状态
1213
+ if (this._readyState === exports.WebSocketState.CLOSING) {
1214
+ this.emit('closing');
1215
+ }
1216
+ }
1217
+ yield new Promise((resolve) => setTimeout(resolve, 2000));
1218
+ this._heathCheckTimer = requestAnimationFrame(this._healthCheck);
1219
+ });
1220
+ /**
1221
+ * 清理健康检查
1222
+ */
1223
+ this._clearHeathCheckIfNeed = () => {
1224
+ if (this._heathCheckTimer) {
1225
+ cancelAnimationFrame(this._heathCheckTimer);
1226
+ this._heathCheckTimer = undefined;
1227
+ }
1228
+ };
1229
+ /**
1230
+ * 开启健康检查
1231
+ */
1232
+ this._startHealthCheck = () => {
1233
+ this._clearHeathCheckIfNeed();
1234
+ this._heathCheckTimer = requestAnimationFrame(this._healthCheck);
1235
+ };
1236
+ /**
1237
+ * 停止健康检查
1238
+ */
1239
+ this._stopHealthCheck = () => {
1240
+ this._clearHeathCheckIfNeed();
1241
+ };
1189
1242
  /**
1190
1243
  * 清理连接中的请求
1191
1244
  */
@@ -1211,7 +1264,7 @@ class RobustWebSocket extends Subscribe {
1211
1264
  this._offline = () => {
1212
1265
  var _a;
1213
1266
  this._reconnectWhenOnlineAgain = true;
1214
- (_a = this._realWebSocket) === null || _a === void 0 ? void 0 : _a.close(1000);
1267
+ (_a = this._realWebSocket) === null || _a === void 0 ? void 0 : _a.close(1000, 'offline');
1215
1268
  };
1216
1269
  /**
1217
1270
  * 解除网络监听事件
@@ -1342,6 +1395,7 @@ class RobustWebSocket extends Subscribe {
1342
1395
  const attempts = this._attempts;
1343
1396
  this._attempts = 0;
1344
1397
  this._reconnectWhenOnlineAgain = false;
1398
+ this._startHealthCheck();
1345
1399
  return this.emit('open', Object.assign(event, { detail: { reconnects, attempts } }));
1346
1400
  };
1347
1401
  /**
@@ -1351,6 +1405,7 @@ class RobustWebSocket extends Subscribe {
1351
1405
  */
1352
1406
  this._webSocketCloseHandler = (event) => {
1353
1407
  this._reconnect(event);
1408
+ this._stopHealthCheck();
1354
1409
  return this.emit('close', event);
1355
1410
  };
1356
1411
  /**
@@ -1372,6 +1427,7 @@ class RobustWebSocket extends Subscribe {
1372
1427
  if (options) {
1373
1428
  this.setOptions(options);
1374
1429
  }
1430
+ this._startHealthCheck();
1375
1431
  }
1376
1432
  get attempts() {
1377
1433
  return this._attempts;
@@ -1422,7 +1478,10 @@ class LiveController extends Subscribe {
1422
1478
  /**
1423
1479
  * ws代理器
1424
1480
  */
1425
- this._ws = new RobustWebSocket();
1481
+ this._ws = new RobustWebSocket({
1482
+ url: '',
1483
+ robustOptions: { timeout: 180000 },
1484
+ });
1426
1485
  /**
1427
1486
  * 带看连接状态
1428
1487
  */
@@ -1432,6 +1491,7 @@ class LiveController extends Subscribe {
1432
1491
  */
1433
1492
  this._addWebSocketEventListeners = () => {
1434
1493
  this._ws.on('close', this._handleCloseEvent);
1494
+ this._ws.on('closing', this._handleClosingEvent);
1435
1495
  this._ws.on('connecting', this._handleConnectingEvent);
1436
1496
  this._ws.on('error', this._handleErrorEvent);
1437
1497
  this._ws.on('message', this._handleMessageEvent);
@@ -1443,6 +1503,7 @@ class LiveController extends Subscribe {
1443
1503
  */
1444
1504
  this._removeWebSocketEventListeners = () => {
1445
1505
  this._ws.off('close', this._handleCloseEvent);
1506
+ this._ws.off('closing', this._handleClosingEvent);
1446
1507
  this._ws.off('connecting', this._handleConnectingEvent);
1447
1508
  this._ws.off('error', this._handleErrorEvent);
1448
1509
  this._ws.off('message', this._handleMessageEvent);
@@ -1457,12 +1518,20 @@ class LiveController extends Subscribe {
1457
1518
  logger$1.info(`live room closed bacause of ${event.code}-${event.reason || parseWSClsErr(event.code)}`);
1458
1519
  this.state = exports.LiveState.CLOSED;
1459
1520
  };
1521
+ /**
1522
+ * 处理房间正在关闭事件(这个时候可能是断网了)
1523
+ */
1524
+ this._handleClosingEvent = () => {
1525
+ var _a;
1526
+ 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);
1527
+ this.state = exports.LiveState.CLOSED;
1528
+ };
1460
1529
  /**
1461
1530
  * 连接中
1462
1531
  * @param event
1463
1532
  */
1464
- this._handleConnectingEvent = (event) => {
1465
- logger$1.info(`live room is connecting`, event.detail);
1533
+ this._handleConnectingEvent = () => {
1534
+ logger$1.info(`live room is connecting`);
1466
1535
  this.state = exports.LiveState.CONNECTING;
1467
1536
  };
1468
1537
  /**
@@ -1625,7 +1694,6 @@ class Live extends Subscribe {
1625
1694
  this._addLiveControllerEventListeners = () => {
1626
1695
  this._controller.on('stateChange', this._handleStateChange);
1627
1696
  this._controller.on('message', this._handleMessage);
1628
- this._controller.on('reconnected', this._handleReconnected);
1629
1697
  };
1630
1698
  /**
1631
1699
  * 移除带看控制的事件监听
@@ -1633,7 +1701,6 @@ class Live extends Subscribe {
1633
1701
  this._removeLiveControllerEventListeners = () => {
1634
1702
  this._controller.off('stateChange', this._handleStateChange);
1635
1703
  this._controller.off('message', this._handleMessage);
1636
- this._controller.off('reconnected', this._handleReconnected);
1637
1704
  };
1638
1705
  /**
1639
1706
  * 带看状态变化
@@ -1788,32 +1855,6 @@ class Live extends Subscribe {
1788
1855
  }
1789
1856
  }
1790
1857
  });
1791
- /**
1792
- * 处理重连
1793
- * @param detail
1794
- */
1795
- this._handleReconnected = (detail) => __awaiter(this, void 0, void 0, function* () {
1796
- var _h, _j, _k, _l, _m, _o, _p;
1797
- // 已经重连上ws,尝试重连语音
1798
- this.$RTC.joined = false;
1799
- const { rtcInfo } = this._buildInData;
1800
- if (rtcInfo) {
1801
- const { voiceId, type } = rtcInfo;
1802
- const result = yield ((_h = this.$RTC) === null || _h === void 0 ? void 0 : _h.join({
1803
- voiceId,
1804
- userId: ((_j = this.selfInfo) === null || _j === void 0 ? void 0 : _j.id) || '',
1805
- roomId: (_k = this.roomInfo) === null || _k === void 0 ? void 0 : _k.info.room_code,
1806
- type,
1807
- }));
1808
- if (result) {
1809
- 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 : exports.MicroStatus.OPEN;
1810
- yield ((_p = this.$RTC) === null || _p === void 0 ? void 0 : _p.toggleMicro(status === exports.MicroStatus.OPEN));
1811
- }
1812
- else {
1813
- logger$1.error('加入语音房间失败');
1814
- }
1815
- }
1816
- });
1817
1858
  /**
1818
1859
  * 清理带看缓存的数据
1819
1860
  */
@@ -1826,12 +1867,12 @@ class Live extends Subscribe {
1826
1867
  * @returns
1827
1868
  */
1828
1869
  this.connect = (options = {}) => __awaiter(this, void 0, void 0, function* () {
1829
- var _q, _r, _s, _t;
1870
+ var _h, _j, _k, _l;
1830
1871
  // 检查 url 和 票据获取接口实现
1831
- this._options.url = (_r = (_q = options.url) !== null && _q !== void 0 ? _q : this._options.url) !== null && _r !== void 0 ? _r : '';
1832
- this._options.getTicket = (_s = options.getTicket) !== null && _s !== void 0 ? _s : this._options.getTicket;
1872
+ this._options.url = (_j = (_h = options.url) !== null && _h !== void 0 ? _h : this._options.url) !== null && _j !== void 0 ? _j : '';
1873
+ this._options.getTicket = (_k = options.getTicket) !== null && _k !== void 0 ? _k : this._options.getTicket;
1833
1874
  this._options.initialMicroStatus =
1834
- (_t = options.rtcMicStatus) !== null && _t !== void 0 ? _t : this._options.initialMicroStatus;
1875
+ (_l = options.rtcMicStatus) !== null && _l !== void 0 ? _l : this._options.initialMicroStatus;
1835
1876
  if (!/^ws(s):\/\//.test(this._options.url) ||
1836
1877
  typeof this._options.getTicket !== 'function') {
1837
1878
  if (!/^ws(s):\/\//.test(this._options.url)) {
@@ -1873,11 +1914,11 @@ class Live extends Subscribe {
1873
1914
  * @returns
1874
1915
  */
1875
1916
  this.exit = () => __awaiter(this, void 0, void 0, function* () {
1876
- var _u;
1917
+ var _m;
1877
1918
  this._clearLiveData();
1878
1919
  try {
1879
1920
  /** 退出强制关闭语音 */
1880
- (_u = this.$RTC) === null || _u === void 0 ? void 0 : _u.quit();
1921
+ (_m = this.$RTC) === null || _m === void 0 ? void 0 : _m.quit();
1881
1922
  yield this.updateStatus(exports.BuiltinEventType.RoomChange, exports.BuiltinActionType.RoomClose, {});
1882
1923
  }
1883
1924
  catch (e) {
@@ -1904,10 +1945,10 @@ class Live extends Subscribe {
1904
1945
  });
1905
1946
  // 仅用于用户自己的麦克风控制
1906
1947
  this.toggleMicro = (microStatus) => __awaiter(this, void 0, void 0, function* () {
1907
- var _v, _w, _x;
1908
- if (!((_w = (_v = this.selfInfo) === null || _v === void 0 ? void 0 : _v.permission) === null || _w === void 0 ? void 0 : _w.control_own_micro))
1948
+ var _o, _p, _q;
1949
+ if (!((_p = (_o = this.selfInfo) === null || _o === void 0 ? void 0 : _o.permission) === null || _p === void 0 ? void 0 : _p.control_own_micro))
1909
1950
  return false;
1910
- const result = yield ((_x = this.$RTC) === null || _x === void 0 ? void 0 : _x.toggleMicro(microStatus));
1951
+ const result = yield ((_q = this.$RTC) === null || _q === void 0 ? void 0 : _q.toggleMicro(microStatus));
1911
1952
  if (result) {
1912
1953
  yield this.updateStatus(exports.BuiltinEventType.UserChange, exports.BuiltinActionType.UserMicroStatus, {
1913
1954
  status: microStatus ? exports.MicroStatus.OPEN : exports.MicroStatus.CLOSE,
@@ -1922,12 +1963,12 @@ class Live extends Subscribe {
1922
1963
  * @returns
1923
1964
  */
1924
1965
  this.forbidMicro = (microStatus, toUserIds) => __awaiter(this, void 0, void 0, function* () {
1925
- var _y, _z;
1966
+ var _r, _s;
1926
1967
  if (typeof this._options.canForbidUserMicro === 'function' &&
1927
1968
  !this._options.canForbidUserMicro(toUserIds)) {
1928
1969
  return false;
1929
1970
  }
1930
- if (!((_z = (_y = this.selfInfo) === null || _y === void 0 ? void 0 : _y.permission) === null || _z === void 0 ? void 0 : _z.control_other_micro)) {
1971
+ if (!((_s = (_r = this.selfInfo) === null || _r === void 0 ? void 0 : _r.permission) === null || _s === void 0 ? void 0 : _s.control_other_micro)) {
1931
1972
  return false;
1932
1973
  }
1933
1974
  return yield this.updateStatus(exports.BuiltinEventType.UserChange, exports.BuiltinActionType.UserMicroStatus, {
@@ -1941,12 +1982,12 @@ class Live extends Subscribe {
1941
1982
  * @returns
1942
1983
  */
1943
1984
  this.forbidSync = (sync, toUserIds) => __awaiter(this, void 0, void 0, function* () {
1944
- var _0, _1;
1985
+ var _t, _u;
1945
1986
  if (typeof this._options.canForbidUserSync === 'function' &&
1946
1987
  !this._options.canForbidUserSync(toUserIds)) {
1947
1988
  return false;
1948
1989
  }
1949
- if (!((_1 = (_0 = this.selfInfo) === null || _0 === void 0 ? void 0 : _0.permission) === null || _1 === void 0 ? void 0 : _1.control_other_sync)) {
1990
+ if (!((_u = (_t = this.selfInfo) === null || _t === void 0 ? void 0 : _t.permission) === null || _u === void 0 ? void 0 : _u.control_other_sync)) {
1950
1991
  return false;
1951
1992
  }
1952
1993
  return yield this.updateStatus(exports.BuiltinEventType.UserChange, exports.BuiltinActionType.UserPermission, { sync_frame: sync }, toUserIds);
@@ -1957,12 +1998,12 @@ class Live extends Subscribe {
1957
1998
  * @returns
1958
1999
  */
1959
2000
  this.kick = (toUserIds) => __awaiter(this, void 0, void 0, function* () {
1960
- var _2, _3;
2001
+ var _v, _w;
1961
2002
  if (typeof this._options.canForbidUserKick === 'function' &&
1962
2003
  !this._options.canForbidUserKick(toUserIds)) {
1963
2004
  return false;
1964
2005
  }
1965
- if (!((_3 = (_2 = this.selfInfo) === null || _2 === void 0 ? void 0 : _2.permission) === null || _3 === void 0 ? void 0 : _3.change_other_status)) {
2006
+ if (!((_w = (_v = this.selfInfo) === null || _v === void 0 ? void 0 : _v.permission) === null || _w === void 0 ? void 0 : _w.change_other_status)) {
1966
2007
  return false;
1967
2008
  }
1968
2009
  return yield this.updateStatus(exports.BuiltinEventType.UserChange, exports.BuiltinActionType.UserStatus, {