@realsee/live 0.5.0-alpha.3 → 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.
@@ -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
  * 解除网络监听事件
@@ -1287,6 +1328,7 @@ class RobustWebSocket extends Subscribe {
1287
1328
  if (!newUrl)
1288
1329
  return;
1289
1330
  this._pendingReconnect = undefined;
1331
+ this._removeWebSocketEvent();
1290
1332
  this._realWebSocket = new WebSocket(newUrl, this._protocols || undefined);
1291
1333
  this._attempts = this._attempts + 1;
1292
1334
  this.emit('connecting', new CustomEvent('connecting', {
@@ -1308,6 +1350,16 @@ class RobustWebSocket extends Subscribe {
1308
1350
  this._attachConnectivityEvents();
1309
1351
  }
1310
1352
  };
1353
+ /**
1354
+ * 断开ws事件
1355
+ */
1356
+ this._removeWebSocketEvent = () => {
1357
+ var _a, _b, _c, _d;
1358
+ (_a = this._realWebSocket) === null || _a === void 0 ? void 0 : _a.removeEventListener('open', this._webSocketOpenHandler);
1359
+ (_b = this._realWebSocket) === null || _b === void 0 ? void 0 : _b.removeEventListener('close', this._webSocketCloseHandler);
1360
+ (_c = this._realWebSocket) === null || _c === void 0 ? void 0 : _c.removeEventListener('error', this._webSocketErrorHandler);
1361
+ (_d = this._realWebSocket) === null || _d === void 0 ? void 0 : _d.removeEventListener('message', this._webSocketMessageHandler);
1362
+ };
1311
1363
  /**
1312
1364
  * 分发ws事件
1313
1365
  */
@@ -1331,6 +1383,7 @@ class RobustWebSocket extends Subscribe {
1331
1383
  const attempts = this._attempts;
1332
1384
  this._attempts = 0;
1333
1385
  this._reconnectWhenOnlineAgain = false;
1386
+ this._startHealthCheck();
1334
1387
  return this.emit('open', Object.assign(event, { detail: { reconnects, attempts } }));
1335
1388
  };
1336
1389
  /**
@@ -1340,6 +1393,7 @@ class RobustWebSocket extends Subscribe {
1340
1393
  */
1341
1394
  this._webSocketCloseHandler = (event) => {
1342
1395
  this._reconnect(event);
1396
+ this._stopHealthCheck();
1343
1397
  return this.emit('close', event);
1344
1398
  };
1345
1399
  /**
@@ -1361,6 +1415,7 @@ class RobustWebSocket extends Subscribe {
1361
1415
  if (options) {
1362
1416
  this.setOptions(options);
1363
1417
  }
1418
+ this._startHealthCheck();
1364
1419
  }
1365
1420
  get attempts() {
1366
1421
  return this._attempts;
@@ -1421,6 +1476,7 @@ class LiveController extends Subscribe {
1421
1476
  */
1422
1477
  this._addWebSocketEventListeners = () => {
1423
1478
  this._ws.on('close', this._handleCloseEvent);
1479
+ this._ws.on('closing', this._handleClosingEvent);
1424
1480
  this._ws.on('connecting', this._handleConnectingEvent);
1425
1481
  this._ws.on('error', this._handleErrorEvent);
1426
1482
  this._ws.on('message', this._handleMessageEvent);
@@ -1432,6 +1488,7 @@ class LiveController extends Subscribe {
1432
1488
  */
1433
1489
  this._removeWebSocketEventListeners = () => {
1434
1490
  this._ws.off('close', this._handleCloseEvent);
1491
+ this._ws.off('closing', this._handleClosingEvent);
1435
1492
  this._ws.off('connecting', this._handleConnectingEvent);
1436
1493
  this._ws.off('error', this._handleErrorEvent);
1437
1494
  this._ws.off('message', this._handleMessageEvent);
@@ -1446,12 +1503,20 @@ class LiveController extends Subscribe {
1446
1503
  logger$1.info(`live room closed bacause of ${event.code}-${event.reason || parseWSClsErr(event.code)}`);
1447
1504
  this.state = exports.LiveState.CLOSED;
1448
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
+ };
1449
1514
  /**
1450
1515
  * 连接中
1451
1516
  * @param event
1452
1517
  */
1453
- this._handleConnectingEvent = (event) => {
1454
- logger$1.info(`live room is connecting`, event.detail);
1518
+ this._handleConnectingEvent = () => {
1519
+ logger$1.info(`live room is connecting`);
1455
1520
  this.state = exports.LiveState.CONNECTING;
1456
1521
  };
1457
1522
  /**
@@ -1485,6 +1550,9 @@ class LiveController extends Subscribe {
1485
1550
  this._handleOpenEvent = (event) => {
1486
1551
  logger$1.info('live room open', event.detail);
1487
1552
  this.state = exports.LiveState.OPEN;
1553
+ if (event.detail.reconnects > 0) {
1554
+ this.emit('reconnected', event.detail);
1555
+ }
1488
1556
  };
1489
1557
  /**
1490
1558
  * 处理超时
@@ -1625,6 +1693,10 @@ class Live extends Subscribe {
1625
1693
  * @returns
1626
1694
  */
1627
1695
  this._handleStateChange = (state, prevState) => this.emit('stateChange', state, prevState);
1696
+ /**
1697
+ * 消息接收
1698
+ * @param liveMsg
1699
+ */
1628
1700
  this._handleMessage = (liveMsg) => __awaiter(this, void 0, void 0, function* () {
1629
1701
  var _b, _c, _d, _e, _f, _g;
1630
1702
  if (typeof liveMsg === 'object') {
@@ -1656,6 +1728,7 @@ class Live extends Subscribe {
1656
1728
  */
1657
1729
  case exports.Command.RTCInfo: {
1658
1730
  const { voice_id: voiceId, type } = data;
1731
+ this._buildInData.rtcInfo = { voiceId, type };
1659
1732
  if (!((_b = this.$RTC) === null || _b === void 0 ? void 0 : _b.joined)) {
1660
1733
  const result = yield ((_c = this.$RTC) === null || _c === void 0 ? void 0 : _c.join({
1661
1734
  voiceId,