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

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');
@@ -1160,6 +1163,7 @@ class RobustWebSocket extends Subscribe {
1160
1163
  constructor(options) {
1161
1164
  // eslint-disable-next-line @typescript-eslint/no-unsafe-call
1162
1165
  super();
1166
+ this._readyState = WebSocketState.NOTINITIALIZED;
1163
1167
  this._options = {
1164
1168
  timeout: 10000,
1165
1169
  shouldReconnect: (event, ws) => {
@@ -1176,6 +1180,43 @@ class RobustWebSocket extends Subscribe {
1176
1180
  this._reconnectWhenOnlineAgain = false;
1177
1181
  this._explicitlyClosed = false;
1178
1182
  this._connectivityEventsAttached = false;
1183
+ /**
1184
+ * 做一个健康检查
1185
+ */
1186
+ this._healthCheck = () => __awaiter(this, void 0, void 0, function* () {
1187
+ if (this._realWebSocket &&
1188
+ this._realWebSocket.readyState !== this._readyState) {
1189
+ this._readyState = this._realWebSocket.readyState;
1190
+ // 太惨了,断网的时候,ws不会直接closed,而是处于closing的状态
1191
+ if (this._readyState === WebSocketState.CLOSING) {
1192
+ this.emit('closing');
1193
+ }
1194
+ }
1195
+ yield new Promise((resolve) => setTimeout(resolve, 2000));
1196
+ this._heathCheckTimer = requestAnimationFrame(this._healthCheck);
1197
+ });
1198
+ /**
1199
+ * 清理健康检查
1200
+ */
1201
+ this._clearHeathCheckIfNeed = () => {
1202
+ if (this._heathCheckTimer) {
1203
+ cancelAnimationFrame(this._heathCheckTimer);
1204
+ this._heathCheckTimer = undefined;
1205
+ }
1206
+ };
1207
+ /**
1208
+ * 开启健康检查
1209
+ */
1210
+ this._startHealthCheck = () => {
1211
+ this._clearHeathCheckIfNeed();
1212
+ this._heathCheckTimer = requestAnimationFrame(this._healthCheck);
1213
+ };
1214
+ /**
1215
+ * 停止健康检查
1216
+ */
1217
+ this._stopHealthCheck = () => {
1218
+ this._clearHeathCheckIfNeed();
1219
+ };
1179
1220
  /**
1180
1221
  * 清理连接中的请求
1181
1222
  */
@@ -1201,7 +1242,7 @@ class RobustWebSocket extends Subscribe {
1201
1242
  this._offline = () => {
1202
1243
  var _a;
1203
1244
  this._reconnectWhenOnlineAgain = true;
1204
- (_a = this._realWebSocket) === null || _a === void 0 ? void 0 : _a.close(1000);
1245
+ (_a = this._realWebSocket) === null || _a === void 0 ? void 0 : _a.close(1000, 'offline');
1205
1246
  };
1206
1247
  /**
1207
1248
  * 解除网络监听事件
@@ -1332,6 +1373,7 @@ class RobustWebSocket extends Subscribe {
1332
1373
  const attempts = this._attempts;
1333
1374
  this._attempts = 0;
1334
1375
  this._reconnectWhenOnlineAgain = false;
1376
+ this._startHealthCheck();
1335
1377
  return this.emit('open', Object.assign(event, { detail: { reconnects, attempts } }));
1336
1378
  };
1337
1379
  /**
@@ -1341,6 +1383,7 @@ class RobustWebSocket extends Subscribe {
1341
1383
  */
1342
1384
  this._webSocketCloseHandler = (event) => {
1343
1385
  this._reconnect(event);
1386
+ this._stopHealthCheck();
1344
1387
  return this.emit('close', event);
1345
1388
  };
1346
1389
  /**
@@ -1362,6 +1405,7 @@ class RobustWebSocket extends Subscribe {
1362
1405
  if (options) {
1363
1406
  this.setOptions(options);
1364
1407
  }
1408
+ this._startHealthCheck();
1365
1409
  }
1366
1410
  get attempts() {
1367
1411
  return this._attempts;
@@ -1422,6 +1466,7 @@ class LiveController extends Subscribe {
1422
1466
  */
1423
1467
  this._addWebSocketEventListeners = () => {
1424
1468
  this._ws.on('close', this._handleCloseEvent);
1469
+ this._ws.on('closing', this._handleClosingEvent);
1425
1470
  this._ws.on('connecting', this._handleConnectingEvent);
1426
1471
  this._ws.on('error', this._handleErrorEvent);
1427
1472
  this._ws.on('message', this._handleMessageEvent);
@@ -1433,6 +1478,7 @@ class LiveController extends Subscribe {
1433
1478
  */
1434
1479
  this._removeWebSocketEventListeners = () => {
1435
1480
  this._ws.off('close', this._handleCloseEvent);
1481
+ this._ws.off('closing', this._handleClosingEvent);
1436
1482
  this._ws.off('connecting', this._handleConnectingEvent);
1437
1483
  this._ws.off('error', this._handleErrorEvent);
1438
1484
  this._ws.off('message', this._handleMessageEvent);
@@ -1447,12 +1493,20 @@ class LiveController extends Subscribe {
1447
1493
  logger$1.info(`live room closed bacause of ${event.code}-${event.reason || parseWSClsErr(event.code)}`);
1448
1494
  this.state = LiveState.CLOSED;
1449
1495
  };
1496
+ /**
1497
+ * 处理房间正在关闭事件(这个时候可能是断网了)
1498
+ */
1499
+ this._handleClosingEvent = () => {
1500
+ var _a;
1501
+ 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);
1502
+ this.state = LiveState.CLOSED;
1503
+ };
1450
1504
  /**
1451
1505
  * 连接中
1452
1506
  * @param event
1453
1507
  */
1454
- this._handleConnectingEvent = (event) => {
1455
- logger$1.info(`live room is connecting`, event.detail);
1508
+ this._handleConnectingEvent = () => {
1509
+ logger$1.info(`live room is connecting`);
1456
1510
  this.state = LiveState.CONNECTING;
1457
1511
  };
1458
1512
  /**
@@ -1615,7 +1669,6 @@ class Live extends Subscribe {
1615
1669
  this._addLiveControllerEventListeners = () => {
1616
1670
  this._controller.on('stateChange', this._handleStateChange);
1617
1671
  this._controller.on('message', this._handleMessage);
1618
- this._controller.on('reconnected', this._handleReconnected);
1619
1672
  };
1620
1673
  /**
1621
1674
  * 移除带看控制的事件监听
@@ -1623,7 +1676,6 @@ class Live extends Subscribe {
1623
1676
  this._removeLiveControllerEventListeners = () => {
1624
1677
  this._controller.off('stateChange', this._handleStateChange);
1625
1678
  this._controller.off('message', this._handleMessage);
1626
- this._controller.off('reconnected', this._handleReconnected);
1627
1679
  };
1628
1680
  /**
1629
1681
  * 带看状态变化
@@ -1778,32 +1830,6 @@ class Live extends Subscribe {
1778
1830
  }
1779
1831
  }
1780
1832
  });
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
1833
  /**
1808
1834
  * 清理带看缓存的数据
1809
1835
  */
@@ -1816,12 +1842,12 @@ class Live extends Subscribe {
1816
1842
  * @returns
1817
1843
  */
1818
1844
  this.connect = (options = {}) => __awaiter(this, void 0, void 0, function* () {
1819
- var _q, _r, _s, _t;
1845
+ var _h, _j, _k, _l;
1820
1846
  // 检查 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;
1847
+ this._options.url = (_j = (_h = options.url) !== null && _h !== void 0 ? _h : this._options.url) !== null && _j !== void 0 ? _j : '';
1848
+ this._options.getTicket = (_k = options.getTicket) !== null && _k !== void 0 ? _k : this._options.getTicket;
1823
1849
  this._options.initialMicroStatus =
1824
- (_t = options.rtcMicStatus) !== null && _t !== void 0 ? _t : this._options.initialMicroStatus;
1850
+ (_l = options.rtcMicStatus) !== null && _l !== void 0 ? _l : this._options.initialMicroStatus;
1825
1851
  if (!/^ws(s):\/\//.test(this._options.url) ||
1826
1852
  typeof this._options.getTicket !== 'function') {
1827
1853
  if (!/^ws(s):\/\//.test(this._options.url)) {
@@ -1863,11 +1889,11 @@ class Live extends Subscribe {
1863
1889
  * @returns
1864
1890
  */
1865
1891
  this.exit = () => __awaiter(this, void 0, void 0, function* () {
1866
- var _u;
1892
+ var _m;
1867
1893
  this._clearLiveData();
1868
1894
  try {
1869
1895
  /** 退出强制关闭语音 */
1870
- (_u = this.$RTC) === null || _u === void 0 ? void 0 : _u.quit();
1896
+ (_m = this.$RTC) === null || _m === void 0 ? void 0 : _m.quit();
1871
1897
  yield this.updateStatus(BuiltinEventType.RoomChange, BuiltinActionType.RoomClose, {});
1872
1898
  }
1873
1899
  catch (e) {
@@ -1894,10 +1920,10 @@ class Live extends Subscribe {
1894
1920
  });
1895
1921
  // 仅用于用户自己的麦克风控制
1896
1922
  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))
1923
+ var _o, _p, _q;
1924
+ if (!((_p = (_o = this.selfInfo) === null || _o === void 0 ? void 0 : _o.permission) === null || _p === void 0 ? void 0 : _p.control_own_micro))
1899
1925
  return false;
1900
- const result = yield ((_x = this.$RTC) === null || _x === void 0 ? void 0 : _x.toggleMicro(microStatus));
1926
+ const result = yield ((_q = this.$RTC) === null || _q === void 0 ? void 0 : _q.toggleMicro(microStatus));
1901
1927
  if (result) {
1902
1928
  yield this.updateStatus(BuiltinEventType.UserChange, BuiltinActionType.UserMicroStatus, {
1903
1929
  status: microStatus ? MicroStatus.OPEN : MicroStatus.CLOSE,
@@ -1912,12 +1938,12 @@ class Live extends Subscribe {
1912
1938
  * @returns
1913
1939
  */
1914
1940
  this.forbidMicro = (microStatus, toUserIds) => __awaiter(this, void 0, void 0, function* () {
1915
- var _y, _z;
1941
+ var _r, _s;
1916
1942
  if (typeof this._options.canForbidUserMicro === 'function' &&
1917
1943
  !this._options.canForbidUserMicro(toUserIds)) {
1918
1944
  return false;
1919
1945
  }
1920
- if (!((_z = (_y = this.selfInfo) === null || _y === void 0 ? void 0 : _y.permission) === null || _z === void 0 ? void 0 : _z.control_other_micro)) {
1946
+ if (!((_s = (_r = this.selfInfo) === null || _r === void 0 ? void 0 : _r.permission) === null || _s === void 0 ? void 0 : _s.control_other_micro)) {
1921
1947
  return false;
1922
1948
  }
1923
1949
  return yield this.updateStatus(BuiltinEventType.UserChange, BuiltinActionType.UserMicroStatus, {
@@ -1931,12 +1957,12 @@ class Live extends Subscribe {
1931
1957
  * @returns
1932
1958
  */
1933
1959
  this.forbidSync = (sync, toUserIds) => __awaiter(this, void 0, void 0, function* () {
1934
- var _0, _1;
1960
+ var _t, _u;
1935
1961
  if (typeof this._options.canForbidUserSync === 'function' &&
1936
1962
  !this._options.canForbidUserSync(toUserIds)) {
1937
1963
  return false;
1938
1964
  }
1939
- if (!((_1 = (_0 = this.selfInfo) === null || _0 === void 0 ? void 0 : _0.permission) === null || _1 === void 0 ? void 0 : _1.control_other_sync)) {
1965
+ if (!((_u = (_t = this.selfInfo) === null || _t === void 0 ? void 0 : _t.permission) === null || _u === void 0 ? void 0 : _u.control_other_sync)) {
1940
1966
  return false;
1941
1967
  }
1942
1968
  return yield this.updateStatus(BuiltinEventType.UserChange, BuiltinActionType.UserPermission, { sync_frame: sync }, toUserIds);
@@ -1947,12 +1973,12 @@ class Live extends Subscribe {
1947
1973
  * @returns
1948
1974
  */
1949
1975
  this.kick = (toUserIds) => __awaiter(this, void 0, void 0, function* () {
1950
- var _2, _3;
1976
+ var _v, _w;
1951
1977
  if (typeof this._options.canForbidUserKick === 'function' &&
1952
1978
  !this._options.canForbidUserKick(toUserIds)) {
1953
1979
  return false;
1954
1980
  }
1955
- if (!((_3 = (_2 = this.selfInfo) === null || _2 === void 0 ? void 0 : _2.permission) === null || _3 === void 0 ? void 0 : _3.change_other_status)) {
1981
+ if (!((_w = (_v = this.selfInfo) === null || _v === void 0 ? void 0 : _v.permission) === null || _w === void 0 ? void 0 : _w.change_other_status)) {
1956
1982
  return false;
1957
1983
  }
1958
1984
  return yield this.updateStatus(BuiltinEventType.UserChange, BuiltinActionType.UserStatus, {