@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.
@@ -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');
@@ -1170,6 +1173,7 @@ class RobustWebSocket extends Subscribe {
1170
1173
  constructor(options) {
1171
1174
  // eslint-disable-next-line @typescript-eslint/no-unsafe-call
1172
1175
  super();
1176
+ this._readyState = exports.WebSocketState.NOTINITIALIZED;
1173
1177
  this._options = {
1174
1178
  timeout: 10000,
1175
1179
  shouldReconnect: (event, ws) => {
@@ -1186,6 +1190,43 @@ class RobustWebSocket extends Subscribe {
1186
1190
  this._reconnectWhenOnlineAgain = false;
1187
1191
  this._explicitlyClosed = false;
1188
1192
  this._connectivityEventsAttached = false;
1193
+ /**
1194
+ * 做一个健康检查
1195
+ */
1196
+ this._healthCheck = () => __awaiter(this, void 0, void 0, function* () {
1197
+ if (this._realWebSocket &&
1198
+ this._realWebSocket.readyState !== this._readyState) {
1199
+ this._readyState = this._realWebSocket.readyState;
1200
+ // 太惨了,断网的时候,ws不会直接closed,而是处于closing的状态
1201
+ if (this._readyState === exports.WebSocketState.CLOSING) {
1202
+ this.emit('closing');
1203
+ }
1204
+ }
1205
+ yield new Promise((resolve) => setTimeout(resolve, 2000));
1206
+ this._heathCheckTimer = requestAnimationFrame(this._healthCheck);
1207
+ });
1208
+ /**
1209
+ * 清理健康检查
1210
+ */
1211
+ this._clearHeathCheckIfNeed = () => {
1212
+ if (this._heathCheckTimer) {
1213
+ cancelAnimationFrame(this._heathCheckTimer);
1214
+ this._heathCheckTimer = undefined;
1215
+ }
1216
+ };
1217
+ /**
1218
+ * 开启健康检查
1219
+ */
1220
+ this._startHealthCheck = () => {
1221
+ this._clearHeathCheckIfNeed();
1222
+ this._heathCheckTimer = requestAnimationFrame(this._healthCheck);
1223
+ };
1224
+ /**
1225
+ * 停止健康检查
1226
+ */
1227
+ this._stopHealthCheck = () => {
1228
+ this._clearHeathCheckIfNeed();
1229
+ };
1189
1230
  /**
1190
1231
  * 清理连接中的请求
1191
1232
  */
@@ -1211,7 +1252,7 @@ class RobustWebSocket extends Subscribe {
1211
1252
  this._offline = () => {
1212
1253
  var _a;
1213
1254
  this._reconnectWhenOnlineAgain = true;
1214
- (_a = this._realWebSocket) === null || _a === void 0 ? void 0 : _a.close(1000);
1255
+ (_a = this._realWebSocket) === null || _a === void 0 ? void 0 : _a.close(1000, 'offline');
1215
1256
  };
1216
1257
  /**
1217
1258
  * 解除网络监听事件
@@ -1342,6 +1383,7 @@ class RobustWebSocket extends Subscribe {
1342
1383
  const attempts = this._attempts;
1343
1384
  this._attempts = 0;
1344
1385
  this._reconnectWhenOnlineAgain = false;
1386
+ this._startHealthCheck();
1345
1387
  return this.emit('open', Object.assign(event, { detail: { reconnects, attempts } }));
1346
1388
  };
1347
1389
  /**
@@ -1351,6 +1393,7 @@ class RobustWebSocket extends Subscribe {
1351
1393
  */
1352
1394
  this._webSocketCloseHandler = (event) => {
1353
1395
  this._reconnect(event);
1396
+ this._stopHealthCheck();
1354
1397
  return this.emit('close', event);
1355
1398
  };
1356
1399
  /**
@@ -1372,6 +1415,7 @@ class RobustWebSocket extends Subscribe {
1372
1415
  if (options) {
1373
1416
  this.setOptions(options);
1374
1417
  }
1418
+ this._startHealthCheck();
1375
1419
  }
1376
1420
  get attempts() {
1377
1421
  return this._attempts;
@@ -1432,6 +1476,7 @@ class LiveController extends Subscribe {
1432
1476
  */
1433
1477
  this._addWebSocketEventListeners = () => {
1434
1478
  this._ws.on('close', this._handleCloseEvent);
1479
+ this._ws.on('closing', this._handleClosingEvent);
1435
1480
  this._ws.on('connecting', this._handleConnectingEvent);
1436
1481
  this._ws.on('error', this._handleErrorEvent);
1437
1482
  this._ws.on('message', this._handleMessageEvent);
@@ -1443,6 +1488,7 @@ class LiveController extends Subscribe {
1443
1488
  */
1444
1489
  this._removeWebSocketEventListeners = () => {
1445
1490
  this._ws.off('close', this._handleCloseEvent);
1491
+ this._ws.off('closing', this._handleClosingEvent);
1446
1492
  this._ws.off('connecting', this._handleConnectingEvent);
1447
1493
  this._ws.off('error', this._handleErrorEvent);
1448
1494
  this._ws.off('message', this._handleMessageEvent);
@@ -1457,12 +1503,20 @@ class LiveController extends Subscribe {
1457
1503
  logger$1.info(`live room closed bacause of ${event.code}-${event.reason || parseWSClsErr(event.code)}`);
1458
1504
  this.state = exports.LiveState.CLOSED;
1459
1505
  };
1506
+ /**
1507
+ * 处理房间正在关闭事件(这个时候可能是断网了)
1508
+ */
1509
+ this._handleClosingEvent = () => {
1510
+ var _a;
1511
+ 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);
1512
+ this.state = exports.LiveState.CLOSED;
1513
+ };
1460
1514
  /**
1461
1515
  * 连接中
1462
1516
  * @param event
1463
1517
  */
1464
- this._handleConnectingEvent = (event) => {
1465
- logger$1.info(`live room is connecting`, event.detail);
1518
+ this._handleConnectingEvent = () => {
1519
+ logger$1.info(`live room is connecting`);
1466
1520
  this.state = exports.LiveState.CONNECTING;
1467
1521
  };
1468
1522
  /**
@@ -1625,7 +1679,6 @@ class Live extends Subscribe {
1625
1679
  this._addLiveControllerEventListeners = () => {
1626
1680
  this._controller.on('stateChange', this._handleStateChange);
1627
1681
  this._controller.on('message', this._handleMessage);
1628
- this._controller.on('reconnected', this._handleReconnected);
1629
1682
  };
1630
1683
  /**
1631
1684
  * 移除带看控制的事件监听
@@ -1633,7 +1686,6 @@ class Live extends Subscribe {
1633
1686
  this._removeLiveControllerEventListeners = () => {
1634
1687
  this._controller.off('stateChange', this._handleStateChange);
1635
1688
  this._controller.off('message', this._handleMessage);
1636
- this._controller.off('reconnected', this._handleReconnected);
1637
1689
  };
1638
1690
  /**
1639
1691
  * 带看状态变化
@@ -1788,32 +1840,6 @@ class Live extends Subscribe {
1788
1840
  }
1789
1841
  }
1790
1842
  });
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
1843
  /**
1818
1844
  * 清理带看缓存的数据
1819
1845
  */
@@ -1826,12 +1852,12 @@ class Live extends Subscribe {
1826
1852
  * @returns
1827
1853
  */
1828
1854
  this.connect = (options = {}) => __awaiter(this, void 0, void 0, function* () {
1829
- var _q, _r, _s, _t;
1855
+ var _h, _j, _k, _l;
1830
1856
  // 检查 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;
1857
+ this._options.url = (_j = (_h = options.url) !== null && _h !== void 0 ? _h : this._options.url) !== null && _j !== void 0 ? _j : '';
1858
+ this._options.getTicket = (_k = options.getTicket) !== null && _k !== void 0 ? _k : this._options.getTicket;
1833
1859
  this._options.initialMicroStatus =
1834
- (_t = options.rtcMicStatus) !== null && _t !== void 0 ? _t : this._options.initialMicroStatus;
1860
+ (_l = options.rtcMicStatus) !== null && _l !== void 0 ? _l : this._options.initialMicroStatus;
1835
1861
  if (!/^ws(s):\/\//.test(this._options.url) ||
1836
1862
  typeof this._options.getTicket !== 'function') {
1837
1863
  if (!/^ws(s):\/\//.test(this._options.url)) {
@@ -1873,11 +1899,11 @@ class Live extends Subscribe {
1873
1899
  * @returns
1874
1900
  */
1875
1901
  this.exit = () => __awaiter(this, void 0, void 0, function* () {
1876
- var _u;
1902
+ var _m;
1877
1903
  this._clearLiveData();
1878
1904
  try {
1879
1905
  /** 退出强制关闭语音 */
1880
- (_u = this.$RTC) === null || _u === void 0 ? void 0 : _u.quit();
1906
+ (_m = this.$RTC) === null || _m === void 0 ? void 0 : _m.quit();
1881
1907
  yield this.updateStatus(exports.BuiltinEventType.RoomChange, exports.BuiltinActionType.RoomClose, {});
1882
1908
  }
1883
1909
  catch (e) {
@@ -1904,10 +1930,10 @@ class Live extends Subscribe {
1904
1930
  });
1905
1931
  // 仅用于用户自己的麦克风控制
1906
1932
  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))
1933
+ var _o, _p, _q;
1934
+ if (!((_p = (_o = this.selfInfo) === null || _o === void 0 ? void 0 : _o.permission) === null || _p === void 0 ? void 0 : _p.control_own_micro))
1909
1935
  return false;
1910
- const result = yield ((_x = this.$RTC) === null || _x === void 0 ? void 0 : _x.toggleMicro(microStatus));
1936
+ const result = yield ((_q = this.$RTC) === null || _q === void 0 ? void 0 : _q.toggleMicro(microStatus));
1911
1937
  if (result) {
1912
1938
  yield this.updateStatus(exports.BuiltinEventType.UserChange, exports.BuiltinActionType.UserMicroStatus, {
1913
1939
  status: microStatus ? exports.MicroStatus.OPEN : exports.MicroStatus.CLOSE,
@@ -1922,12 +1948,12 @@ class Live extends Subscribe {
1922
1948
  * @returns
1923
1949
  */
1924
1950
  this.forbidMicro = (microStatus, toUserIds) => __awaiter(this, void 0, void 0, function* () {
1925
- var _y, _z;
1951
+ var _r, _s;
1926
1952
  if (typeof this._options.canForbidUserMicro === 'function' &&
1927
1953
  !this._options.canForbidUserMicro(toUserIds)) {
1928
1954
  return false;
1929
1955
  }
1930
- if (!((_z = (_y = this.selfInfo) === null || _y === void 0 ? void 0 : _y.permission) === null || _z === void 0 ? void 0 : _z.control_other_micro)) {
1956
+ if (!((_s = (_r = this.selfInfo) === null || _r === void 0 ? void 0 : _r.permission) === null || _s === void 0 ? void 0 : _s.control_other_micro)) {
1931
1957
  return false;
1932
1958
  }
1933
1959
  return yield this.updateStatus(exports.BuiltinEventType.UserChange, exports.BuiltinActionType.UserMicroStatus, {
@@ -1941,12 +1967,12 @@ class Live extends Subscribe {
1941
1967
  * @returns
1942
1968
  */
1943
1969
  this.forbidSync = (sync, toUserIds) => __awaiter(this, void 0, void 0, function* () {
1944
- var _0, _1;
1970
+ var _t, _u;
1945
1971
  if (typeof this._options.canForbidUserSync === 'function' &&
1946
1972
  !this._options.canForbidUserSync(toUserIds)) {
1947
1973
  return false;
1948
1974
  }
1949
- if (!((_1 = (_0 = this.selfInfo) === null || _0 === void 0 ? void 0 : _0.permission) === null || _1 === void 0 ? void 0 : _1.control_other_sync)) {
1975
+ if (!((_u = (_t = this.selfInfo) === null || _t === void 0 ? void 0 : _t.permission) === null || _u === void 0 ? void 0 : _u.control_other_sync)) {
1950
1976
  return false;
1951
1977
  }
1952
1978
  return yield this.updateStatus(exports.BuiltinEventType.UserChange, exports.BuiltinActionType.UserPermission, { sync_frame: sync }, toUserIds);
@@ -1957,12 +1983,12 @@ class Live extends Subscribe {
1957
1983
  * @returns
1958
1984
  */
1959
1985
  this.kick = (toUserIds) => __awaiter(this, void 0, void 0, function* () {
1960
- var _2, _3;
1986
+ var _v, _w;
1961
1987
  if (typeof this._options.canForbidUserKick === 'function' &&
1962
1988
  !this._options.canForbidUserKick(toUserIds)) {
1963
1989
  return false;
1964
1990
  }
1965
- if (!((_3 = (_2 = this.selfInfo) === null || _2 === void 0 ? void 0 : _2.permission) === null || _3 === void 0 ? void 0 : _3.change_other_status)) {
1991
+ if (!((_w = (_v = this.selfInfo) === null || _v === void 0 ? void 0 : _v.permission) === null || _w === void 0 ? void 0 : _w.change_other_status)) {
1966
1992
  return false;
1967
1993
  }
1968
1994
  return yield this.updateStatus(exports.BuiltinEventType.UserChange, exports.BuiltinActionType.UserStatus, {