@realsee/live 0.5.0-alpha.4 → 0.5.0-alpha.7

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,9 +1,11 @@
1
1
  import { LiveMsg } from '../typings';
2
2
  import { LiveState } from '../typings/live/LiveState';
3
+ import { RobustWebSocketEventDetail } from '../utils/RobustWebSocket';
3
4
  import { Subscribe } from '../utils/subscribe';
4
5
  export declare type LiveControllerEventMap = {
5
6
  stateChange: (state: LiveState, prevState: LiveState) => void;
6
7
  message: (message: LiveMsg) => void;
8
+ reconnected: (detail: RobustWebSocketEventDetail) => void;
7
9
  };
8
10
  export declare class LiveController extends Subscribe<LiveControllerEventMap> {
9
11
  /**
@@ -30,6 +32,10 @@ export declare class LiveController extends Subscribe<LiveControllerEventMap> {
30
32
  * @param event
31
33
  */
32
34
  private _handleCloseEvent;
35
+ /**
36
+ * 处理房间正在关闭事件(这个时候可能是断网了)
37
+ */
38
+ private _handleClosingEvent;
33
39
  /**
34
40
  * 连接中
35
41
  * @param event
@@ -1,12 +1,16 @@
1
1
  import { JSBridgeProtocol } from '@realsee/jsbridge-x';
2
2
  import { RTCProtocol } from '../rtc';
3
- import { EventType, KeyframeTypes, LiveOptions, RoomInfo, UserInfo, BuiltinEventType, BuiltinActionType, MicroStatus, LiveConnectOptions } from '../typings';
3
+ import { EventType, KeyframeTypes, LiveOptions, RoomInfo, RTCType, UserInfo, BuiltinEventType, BuiltinActionType, MicroStatus, LiveConnectOptions } from '../typings';
4
4
  import { LiveState } from '../typings/live/LiveState';
5
5
  import { Subscribe } from '../utils/subscribe';
6
6
  export declare type LiveBuiltInData<KeyframesSnapshot> = {
7
7
  selfInfo?: UserInfo;
8
8
  userList?: UserInfo[];
9
9
  roomInfo?: RoomInfo;
10
+ rtcInfo?: {
11
+ voiceId: string;
12
+ type: RTCType;
13
+ };
10
14
  keyframes: Partial<KeyframesSnapshot>;
11
15
  };
12
16
  export declare const _clearLiveInstanceIfNeed: () => void;
@@ -94,6 +98,10 @@ export declare class Live<KeyframesSnapshot extends Record<string, any>> extends
94
98
  * @returns
95
99
  */
96
100
  private _handleStateChange;
101
+ /**
102
+ * 消息接收
103
+ * @param liveMsg
104
+ */
97
105
  private _handleMessage;
98
106
  /**
99
107
  * 清理带看缓存的数据
@@ -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;
@@ -1422,7 +1466,10 @@ class LiveController extends Subscribe {
1422
1466
  /**
1423
1467
  * ws代理器
1424
1468
  */
1425
- this._ws = new RobustWebSocket();
1469
+ this._ws = new RobustWebSocket({
1470
+ url: '',
1471
+ robustOptions: { timeout: 180000 },
1472
+ });
1426
1473
  /**
1427
1474
  * 带看连接状态
1428
1475
  */
@@ -1432,6 +1479,7 @@ class LiveController extends Subscribe {
1432
1479
  */
1433
1480
  this._addWebSocketEventListeners = () => {
1434
1481
  this._ws.on('close', this._handleCloseEvent);
1482
+ this._ws.on('closing', this._handleClosingEvent);
1435
1483
  this._ws.on('connecting', this._handleConnectingEvent);
1436
1484
  this._ws.on('error', this._handleErrorEvent);
1437
1485
  this._ws.on('message', this._handleMessageEvent);
@@ -1443,6 +1491,7 @@ class LiveController extends Subscribe {
1443
1491
  */
1444
1492
  this._removeWebSocketEventListeners = () => {
1445
1493
  this._ws.off('close', this._handleCloseEvent);
1494
+ this._ws.off('closing', this._handleClosingEvent);
1446
1495
  this._ws.off('connecting', this._handleConnectingEvent);
1447
1496
  this._ws.off('error', this._handleErrorEvent);
1448
1497
  this._ws.off('message', this._handleMessageEvent);
@@ -1457,12 +1506,20 @@ class LiveController extends Subscribe {
1457
1506
  logger$1.info(`live room closed bacause of ${event.code}-${event.reason || parseWSClsErr(event.code)}`);
1458
1507
  this.state = exports.LiveState.CLOSED;
1459
1508
  };
1509
+ /**
1510
+ * 处理房间正在关闭事件(这个时候可能是断网了)
1511
+ */
1512
+ this._handleClosingEvent = () => {
1513
+ var _a;
1514
+ 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);
1515
+ this.state = exports.LiveState.CLOSED;
1516
+ };
1460
1517
  /**
1461
1518
  * 连接中
1462
1519
  * @param event
1463
1520
  */
1464
- this._handleConnectingEvent = (event) => {
1465
- logger$1.info(`live room is connecting`, event.detail);
1521
+ this._handleConnectingEvent = () => {
1522
+ logger$1.info(`live room is connecting`);
1466
1523
  this.state = exports.LiveState.CONNECTING;
1467
1524
  };
1468
1525
  /**
@@ -1496,6 +1553,9 @@ class LiveController extends Subscribe {
1496
1553
  this._handleOpenEvent = (event) => {
1497
1554
  logger$1.info('live room open', event.detail);
1498
1555
  this.state = exports.LiveState.OPEN;
1556
+ if (event.detail.reconnects > 0) {
1557
+ this.emit('reconnected', event.detail);
1558
+ }
1499
1559
  };
1500
1560
  /**
1501
1561
  * 处理超时
@@ -1636,6 +1696,10 @@ class Live extends Subscribe {
1636
1696
  * @returns
1637
1697
  */
1638
1698
  this._handleStateChange = (state, prevState) => this.emit('stateChange', state, prevState);
1699
+ /**
1700
+ * 消息接收
1701
+ * @param liveMsg
1702
+ */
1639
1703
  this._handleMessage = (liveMsg) => __awaiter(this, void 0, void 0, function* () {
1640
1704
  var _b, _c, _d, _e, _f, _g;
1641
1705
  if (typeof liveMsg === 'object') {
@@ -1667,6 +1731,7 @@ class Live extends Subscribe {
1667
1731
  */
1668
1732
  case exports.Command.RTCInfo: {
1669
1733
  const { voice_id: voiceId, type } = data;
1734
+ this._buildInData.rtcInfo = { voiceId, type };
1670
1735
  if (!((_b = this.$RTC) === null || _b === void 0 ? void 0 : _b.joined)) {
1671
1736
  const result = yield ((_c = this.$RTC) === null || _c === void 0 ? void 0 : _c.join({
1672
1737
  voiceId,