@realsee/live 0.5.0-alpha.8 → 0.5.0-alpha.9

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.
@@ -49245,38 +49245,48 @@
49245
49245
  WeappBridgeMessage["alive"] = "I`m alive!";
49246
49246
  })(WeappBridgeMessage || (WeappBridgeMessage = {}));
49247
49247
 
49248
- const endpoint = 'wss://ws.realsee.com';
49249
- var defaultWsEndpoint = endpoint;
49250
-
49251
49248
  /**
49252
- * 拼接ws链接
49253
- * @param tpl
49254
- * @param params
49255
- * @returns
49249
+ * ws的状态
49250
+ * 参考https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
49256
49251
  */
49257
- const generateWssUrl = (tpl, params) => {
49258
- const url = Object.keys(params).reduce((res, curr) => res.replace(`{{${curr}}}`, `${params[curr]}`), tpl);
49259
- return encodeURI(url);
49260
- };
49261
- var generateWssUrl$1 = generateWssUrl;
49262
-
49263
- const isValidKey = (key, object) => key in object;
49264
- var isValidKey$1 = isValidKey;
49252
+ var WebSocketState;
49253
+ (function (WebSocketState) {
49254
+ WebSocketState[WebSocketState["CONNECTING"] = 0] = "CONNECTING";
49255
+ WebSocketState[WebSocketState["OPEN"] = 1] = "OPEN";
49256
+ WebSocketState[WebSocketState["CLOSING"] = 2] = "CLOSING";
49257
+ WebSocketState[WebSocketState["CLOSED"] = 3] = "CLOSED";
49258
+ })(WebSocketState || (WebSocketState = {}));
49265
49259
 
49266
49260
  /**
49267
- * 解析参数
49268
- * @param query
49269
- * @returns
49261
+ * ws 关闭的原因
49270
49262
  */
49271
- const parseQuery = (query = '') => query
49272
- .replace('?', '')
49273
- .split('&')
49274
- .map((qs) => {
49275
- const [k, v] = qs.split('=');
49276
- return { [k]: decodeURIComponent(v) };
49277
- })
49278
- .reduce((result, curr) => ({ ...result, ...curr }), {});
49279
- var parseQuery$1 = parseQuery;
49263
+ const WebSocketCloseOptions = {
49264
+ /**
49265
+ * 重连关闭
49266
+ */
49267
+ reconnect: { code: 1000, reason: 'close websocket when reconnecting' },
49268
+ /**
49269
+ * 主动关闭
49270
+ */
49271
+ normalClosure: { code: 1000, reason: 'normal closure' },
49272
+ /**
49273
+ * 强制关闭
49274
+ */
49275
+ forceClosure: { code: 1000, reason: 'force closure' },
49276
+ /**
49277
+ * 切换到后台,被微信杀掉,需要重连连接
49278
+ */
49279
+ interrupted: { code: 1000, reason: 'interrupted' },
49280
+ /**
49281
+ * 服务端拒绝连接
49282
+ */
49283
+ streamEndEncountered: { code: 1000, reason: 'stream end encountered' },
49284
+ /**
49285
+ * 丢失心跳
49286
+ * @deprecated
49287
+ */
49288
+ lostHeartbeat: { code: 1000, reason: 'lost heartbeat' },
49289
+ };
49280
49290
 
49281
49291
  const EVENT_SYMBOL = '$$REALSEE_JSBRIDGE_EVENT$$';
49282
49292
  function __generateEventIfNotExisted(instance) {
@@ -49391,269 +49401,228 @@
49391
49401
  }
49392
49402
 
49393
49403
  /**
49394
- * 具有鲁棒性的WebSocket封装
49395
- * 1、自动重连
49396
- * 2、可根据网络情况进行重连
49404
+ * ws的事件
49397
49405
  */
49398
- class RobustWebSocket$1 extends Subscribe {
49399
- constructor(options) {
49400
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call
49401
- super();
49402
- this._options = {
49403
- timeout: 10000,
49404
- shouldReconnect: (event, ws) => {
49405
- if (event.code === 1008 || event.code === 1011)
49406
- return;
49407
- return [0, 3000, 10000][ws.attempts];
49408
- },
49409
- should1000Reconnect: false,
49410
- ignoreConnectivityEvents: false,
49411
- automaticOpen: false,
49412
- };
49413
- this._attempts = 0;
49414
- this._reconnects = -1;
49415
- this._reconnectWhenOnlineAgain = false;
49416
- this._explicitlyClosed = false;
49417
- this._connectivityEventsAttached = false;
49418
- /**
49419
- * 清理连接中的请求
49420
- */
49421
- this._clearPendingReconnectIfNeeded = () => {
49422
- if (this._pendingReconnect) {
49423
- clearTimeout(this._pendingReconnect);
49424
- this._pendingReconnect = undefined;
49425
- }
49426
- };
49427
- /**
49428
- * 当网页连接上网络进行重连
49429
- * @param event
49430
- */
49431
- this._online = (event) => {
49432
- if (this._reconnectWhenOnlineAgain) {
49433
- this._clearPendingReconnectIfNeeded();
49434
- this._reconnect(event);
49435
- }
49436
- };
49437
- /**
49438
- * 当网页断开网络进行关闭
49439
- */
49440
- this._offline = () => {
49441
- var _a;
49442
- this._reconnectWhenOnlineAgain = true;
49443
- (_a = this._realWebSocket) === null || _a === void 0 ? void 0 : _a.close(1000);
49444
- };
49406
+ var WebSocketEvent;
49407
+ (function (WebSocketEvent) {
49408
+ WebSocketEvent["open"] = "open";
49409
+ WebSocketEvent["close"] = "close";
49410
+ WebSocketEvent["error"] = "error";
49411
+ WebSocketEvent["message"] = "message";
49412
+ })(WebSocketEvent || (WebSocketEvent = {}));
49413
+
49414
+ class Ws4Webview extends Subscribe {
49415
+ constructor() {
49416
+ super(...arguments);
49417
+ this._state = null;
49445
49418
  /**
49446
- * 解除网络监听事件
49419
+ * @description 注册ws接通事件回调
49420
+ * @param listener 事件回调函数
49421
+ * @param once 是否只执行一次
49422
+ * @returns
49447
49423
  */
49448
- this._detachConnectivityEvents = () => {
49449
- if (this._connectivityEventsAttached) {
49450
- globalThis === null || globalThis === void 0 ? void 0 : globalThis.removeEventListener('online', this._online);
49451
- globalThis === null || globalThis === void 0 ? void 0 : globalThis.removeEventListener('offline', this._offline);
49452
- this._connectivityEventsAttached = false;
49453
- }
49454
- };
49424
+ this.onOpen = (listener, once) => once ? this.once(WebSocketEvent.open, listener) : this.on(WebSocketEvent.open, listener);
49455
49425
  /**
49456
- * 注册网络监听事件
49426
+ * @description 注册一次ws接通事件回调
49427
+ * @param listener 事件回调函数
49457
49428
  */
49458
- this._attachConnectivityEvents = () => {
49459
- if (!this._connectivityEventsAttached) {
49460
- globalThis === null || globalThis === void 0 ? void 0 : globalThis.addEventListener('online', this._online);
49461
- globalThis === null || globalThis === void 0 ? void 0 : globalThis.addEventListener('offline', this._offline);
49462
- this._connectivityEventsAttached = true;
49463
- }
49464
- };
49429
+ this.onceOpen = (listener) => this.onOpen(listener, true);
49465
49430
  /**
49466
- * 发送消息
49467
- * @param data
49431
+ * @description 移除ws接通事件回调
49432
+ * @param listener 事件回调函数
49468
49433
  * @returns
49469
49434
  */
49470
- this.send = (data) => {
49471
- var _a, _b, _c;
49472
- if (((_a = this._realWebSocket) === null || _a === void 0 ? void 0 : _a.readyState) === ((_b = this._realWebSocket) === null || _b === void 0 ? void 0 : _b.OPEN)) {
49473
- (_c = this._realWebSocket) === null || _c === void 0 ? void 0 : _c.send(data);
49474
- }
49475
- };
49435
+ this.offOpen = (listener) => this.hasListener(WebSocketEvent.open) && this.off(WebSocketEvent.open, listener);
49476
49436
  /**
49477
- * 关闭连接
49478
- * @param code 代码
49479
- * @param reason 原因
49437
+ * @description 注册ws消息事件回调
49438
+ * @param listener 事件回调函数
49439
+ * @param once 是否只执行一次
49480
49440
  * @returns
49481
49441
  */
49482
- this.close = (code, reason) => {
49483
- var _a;
49484
- if (typeof code !== 'number') {
49485
- reason = code;
49486
- code = 1000;
49487
- }
49488
- this._clearPendingReconnectIfNeeded();
49489
- this._reconnectWhenOnlineAgain = false;
49490
- this._explicitlyClosed = true;
49491
- this._detachConnectivityEvents();
49492
- return (_a = this._realWebSocket) === null || _a === void 0 ? void 0 : _a.close(code, reason);
49493
- };
49442
+ this.onMessage = (listener, once) => once ? this.once(WebSocketEvent.message, listener) : this.on(WebSocketEvent.message, listener);
49494
49443
  /**
49495
- * 进行ws连接
49444
+ * @description 注册一次ws消息事件回调
49445
+ * @param listener 事件回调函数
49496
49446
  */
49497
- this.open = (url) => {
49498
- var _a, _b;
49499
- if (url) {
49500
- this._url = url;
49501
- }
49502
- if (((_a = this._realWebSocket) === null || _a === void 0 ? void 0 : _a.readyState) !== WebSocket.OPEN &&
49503
- ((_b = this._realWebSocket) === null || _b === void 0 ? void 0 : _b.readyState) !== WebSocket.CONNECTING) {
49504
- this._clearPendingReconnectIfNeeded();
49505
- this._reconnectWhenOnlineAgain = false;
49506
- this._explicitlyClosed = false;
49507
- this._createWebSocket();
49508
- }
49509
- };
49447
+ this.onceMessage = (listener) => this.onMessage(listener, true);
49510
49448
  /**
49511
- * 创建ws连接
49449
+ * @description 移除ws接通事件回调
49450
+ * @param listener 事件回调函数
49451
+ * @returns
49512
49452
  */
49513
- this._createWebSocket = () => {
49514
- var _a, _b, _c;
49515
- const newUrl = typeof this._url === 'string' ? this._url : (_a = this._url) === null || _a === void 0 ? void 0 : _a.call(this, this);
49516
- if (!newUrl)
49517
- return;
49518
- this._pendingReconnect = undefined;
49519
- this._removeWebSocketEvent();
49520
- this._realWebSocket = new WebSocket(newUrl, this._protocols || undefined);
49521
- this._attempts = this._attempts + 1;
49522
- this.emit('connecting', new CustomEvent('connecting', {
49523
- detail: { attempts: this._attempts, reconnects: this._reconnects },
49524
- }));
49525
- const timeoutCallback = () => {
49526
- this._connectTimeout = undefined;
49527
- this._detachConnectivityEvents();
49528
- this.emit('timeout', new CustomEvent('connecting', {
49529
- detail: {
49530
- attempts: this._attempts,
49531
- reconnects: this._reconnects,
49532
- },
49533
- }));
49534
- };
49535
- this._connectTimeout = setTimeout(timeoutCallback, (_b = this._options) === null || _b === void 0 ? void 0 : _b.timeout);
49536
- this._dispatchWebSocketEvent();
49537
- if (!((_c = this._options) === null || _c === void 0 ? void 0 : _c.ignoreConnectivityEvents)) {
49538
- this._attachConnectivityEvents();
49539
- }
49540
- };
49453
+ this.offMessage = (listener) => this.hasListener(WebSocketEvent.message) && this.off(WebSocketEvent.message, listener);
49541
49454
  /**
49542
- * 断开ws事件
49455
+ * @description 注册ws错误事件回调
49456
+ * @param listener 事件回调函数
49457
+ * @param once 是否只执行一次
49458
+ * @returns
49543
49459
  */
49544
- this._removeWebSocketEvent = () => {
49545
- var _a, _b, _c, _d;
49546
- (_a = this._realWebSocket) === null || _a === void 0 ? void 0 : _a.removeEventListener('open', this._webSocketOpenHandler);
49547
- (_b = this._realWebSocket) === null || _b === void 0 ? void 0 : _b.removeEventListener('close', this._webSocketCloseHandler);
49548
- (_c = this._realWebSocket) === null || _c === void 0 ? void 0 : _c.removeEventListener('error', this._webSocketErrorHandler);
49549
- (_d = this._realWebSocket) === null || _d === void 0 ? void 0 : _d.removeEventListener('message', this._webSocketMessageHandler);
49550
- };
49460
+ this.onError = (listener, once) => once ? this.once(WebSocketEvent.error, listener) : this.on(WebSocketEvent.error, listener);
49551
49461
  /**
49552
- * 分发ws事件
49462
+ * @description 注册一次ws消息事件回调
49463
+ * @param listener 事件回调函数
49553
49464
  */
49554
- this._dispatchWebSocketEvent = () => {
49555
- var _a, _b, _c, _d;
49556
- (_a = this._realWebSocket) === null || _a === void 0 ? void 0 : _a.addEventListener('open', this._webSocketOpenHandler);
49557
- (_b = this._realWebSocket) === null || _b === void 0 ? void 0 : _b.addEventListener('close', this._webSocketCloseHandler);
49558
- (_c = this._realWebSocket) === null || _c === void 0 ? void 0 : _c.addEventListener('error', this._webSocketErrorHandler);
49559
- (_d = this._realWebSocket) === null || _d === void 0 ? void 0 : _d.addEventListener('message', this._webSocketMessageHandler);
49560
- };
49465
+ this.onceError = (listener) => this.onError(listener, true);
49561
49466
  /**
49562
- * ws连接事件
49467
+ * @description 移除ws接通事件回调
49468
+ * @param listener 事件回调函数
49563
49469
  * @returns
49564
49470
  */
49565
- this._webSocketOpenHandler = (event) => {
49566
- if (this._connectTimeout) {
49567
- clearTimeout(this._connectTimeout);
49568
- this._connectTimeout = undefined;
49569
- }
49570
- const reconnects = ++this._reconnects;
49571
- const attempts = this._attempts;
49572
- this._attempts = 0;
49573
- this._reconnectWhenOnlineAgain = false;
49574
- return this.emit('open', Object.assign(event, { detail: { reconnects, attempts } }));
49575
- };
49471
+ this.offError = (listener) => this.hasListener(WebSocketEvent.error) && this.off(WebSocketEvent.error, listener);
49576
49472
  /**
49577
- * ws 关闭事件
49578
- * @param event
49473
+ * @description 注册ws错误事件回调
49474
+ * @param listener 事件回调函数
49475
+ * @param once 是否只执行一次
49579
49476
  * @returns
49580
49477
  */
49581
- this._webSocketCloseHandler = (event) => {
49582
- this._reconnect(event);
49583
- return this.emit('close', event);
49584
- };
49478
+ this.onClose = (listener, once) => once ? this.once(WebSocketEvent.close, listener) : this.on(WebSocketEvent.close, listener);
49585
49479
  /**
49586
- * ws 异常
49587
- * @param event
49588
- * @returns
49480
+ * @description 注册一次ws消息事件回调
49481
+ * @param listener 事件回调函数
49589
49482
  */
49590
- this._webSocketErrorHandler = (event) => {
49591
- return this.emit('error', new CustomEvent('error', { detail: { error: event } }));
49592
- };
49483
+ this.onceClose = (listener) => this.onClose(listener, true);
49593
49484
  /**
49594
- * ws消息
49595
- * @param event
49485
+ * @description 移除ws接通事件回调
49486
+ * @param listener 事件回调函数
49596
49487
  * @returns
49597
49488
  */
49598
- this._webSocketMessageHandler = (event) => {
49599
- return this.emit('message', event);
49600
- };
49601
- if (options) {
49602
- this.setOptions(options);
49603
- }
49489
+ this.offClose = (listener) => this.hasListener(WebSocketEvent.close) && this.off(WebSocketEvent.close, listener);
49604
49490
  }
49605
- get attempts() {
49606
- return this._attempts;
49491
+ get readyState() {
49492
+ return this._state;
49607
49493
  }
49608
- setOptions(options) {
49609
- if (!options)
49610
- return;
49611
- this._url = options.url;
49612
- this._protocols = options.protocols;
49613
- const opts = Object.assign(this._options, options.robustOptions || {});
49614
- if (typeof (opts === null || opts === void 0 ? void 0 : opts.timeout) !== 'number') {
49615
- throw new Error('timeout must be the number of milliseconds to timeout a connection attempt');
49616
- }
49617
- if (typeof (opts === null || opts === void 0 ? void 0 : opts.shouldReconnect) !== 'function') {
49618
- throw new Error('shouldReconnect must be a function that returns the number of milliseconds to wait for a reconnect attempt, or null or undefined to not reconnect.');
49619
- }
49620
- this._options = opts;
49621
- if (this._options.automaticOpen) {
49622
- this._createWebSocket();
49623
- }
49494
+ /**
49495
+ * 连接ws
49496
+ * @param options
49497
+ */
49498
+ connect(options) {
49499
+ if (this._state === WebSocketState.CONNECTING || this._state === WebSocketState.OPEN)
49500
+ return Promise.reject("\u5DF2\u5B58\u5728\u4E00\u4E2A\u8FDE\u63A5\u4E2D\u6216\u6253\u5F00\u72B6\u6001\u7684websocket\u5B9E\u4F8B" /* ErrorMessage.WsAlreadyExists */);
49501
+ return new Promise((resolve, reject) => {
49502
+ const { url } = options;
49503
+ if (!url)
49504
+ throw new Error("\u975E\u6CD5\u7684websocket\u94FE\u63A5" /* ErrorMessage.WsUrlInvalid */);
49505
+ this._wsOptions = options;
49506
+ this._state = WebSocketState.CONNECTING;
49507
+ this._wsTask = new WebSocket(url);
49508
+ this.registerEventHandler();
49509
+ this.handleConnected();
49510
+ this.onceOpen(() => resolve(true));
49511
+ this.onceError((err) => reject(err));
49512
+ });
49624
49513
  }
49625
49514
  /**
49626
- * 进行重连
49627
- * @param event
49515
+ * 重连websocket
49628
49516
  * @returns
49629
49517
  */
49630
- _reconnect(event) {
49631
- var _a, _b, _c, _d;
49632
- if ((!((_a = this._options) === null || _a === void 0 ? void 0 : _a.should1000Reconnect) && event.code === 1000) ||
49633
- this._explicitlyClosed) {
49634
- this._attempts = 0;
49635
- return;
49636
- }
49637
- if (((_b = globalThis === null || globalThis === void 0 ? void 0 : globalThis.navigator) === null || _b === void 0 ? void 0 : _b.onLine) === false) {
49638
- this._reconnectWhenOnlineAgain = true;
49639
- return;
49518
+ async reconnect() {
49519
+ this.close(WebSocketCloseOptions.reconnect);
49520
+ if (this._wsOptions)
49521
+ this.connect(this._wsOptions);
49522
+ }
49523
+ /**
49524
+ * @description 发送消息
49525
+ * @param data 消息体
49526
+ */
49527
+ send(data) {
49528
+ try {
49529
+ if (this._wsTask && this.readyState === WebSocketState.OPEN) {
49530
+ this._wsTask.send(data);
49531
+ }
49532
+ else if (this._wsTask && this.readyState === WebSocketState.CONNECTING) {
49533
+ this.once(WebSocketEvent.open, () => { var _a; return (_a = this._wsTask) === null || _a === void 0 ? void 0 : _a.send(data); });
49534
+ }
49535
+ else {
49536
+ this.once(WebSocketEvent.open, () => this.send(data));
49537
+ this.reconnect();
49538
+ }
49640
49539
  }
49641
- const delay = (_d = (_c = this._options) === null || _c === void 0 ? void 0 : _c.shouldReconnect) === null || _d === void 0 ? void 0 : _d.call(_c, event, this);
49642
- if (typeof delay === 'number') {
49643
- this._pendingReconnect = setTimeout(this._createWebSocket, delay);
49540
+ catch (e) {
49541
+ this.once(WebSocketEvent.open, () => this.send(data));
49542
+ this.reconnect();
49644
49543
  }
49645
49544
  }
49646
- }
49545
+ /**
49546
+ * @description 手动关闭ws
49547
+ * @param options 关闭ws的配置参数
49548
+ */
49549
+ close(options) {
49550
+ var _a;
49551
+ this._state = WebSocketState.CLOSING;
49552
+ (_a = this._wsTask) === null || _a === void 0 ? void 0 : _a.close(options.code, options.reason);
49553
+ }
49554
+ /**
49555
+ * 处理连接后的事件绑定
49556
+ */
49557
+ handleConnected() {
49558
+ this.onceOpen(() => (this._state = WebSocketState.OPEN));
49559
+ this.onClose(() => (this._state = WebSocketState.CLOSED));
49560
+ }
49561
+ /**
49562
+ * 注册websocket事件
49563
+ */
49564
+ registerEventHandler() {
49565
+ var _a, _b, _c, _d;
49566
+ (_a = this._wsTask) === null || _a === void 0 ? void 0 : _a.addEventListener(WebSocketEvent.open, (ev) => this.emit(WebSocketEvent.open, ev));
49567
+ (_b = this._wsTask) === null || _b === void 0 ? void 0 : _b.addEventListener(WebSocketEvent.message, (result) => {
49568
+ let data = result.data;
49569
+ try {
49570
+ if (typeof result.data === 'string') {
49571
+ data = JSON.parse(result.data);
49572
+ }
49573
+ }
49574
+ catch (e) {
49575
+ /** 不可序列化的数据不用处理 */
49576
+ }
49577
+ this.emit(WebSocketEvent.message, data);
49578
+ });
49579
+ (_c = this._wsTask) === null || _c === void 0 ? void 0 : _c.addEventListener(WebSocketEvent.close, (ev) => this.emit(WebSocketEvent.close, ev));
49580
+ (_d = this._wsTask) === null || _d === void 0 ? void 0 : _d.addEventListener(WebSocketEvent.error, (ev) => this.emit(WebSocketEvent.error, ev));
49581
+ }
49582
+ }
49583
+ var Ws4Webview$1 = Ws4Webview;
49584
+
49585
+ const endpoint = 'wss://ws.realsee.com';
49586
+ var defaultWsEndpoint = endpoint;
49647
49587
 
49588
+ /**
49589
+ * 拼接ws链接
49590
+ * @param tpl
49591
+ * @param params
49592
+ * @returns
49593
+ */
49594
+ const generateWssUrl = (tpl, params) => {
49595
+ const url = Object.keys(params).reduce((res, curr) => res.replace(`{{${curr}}}`, `${params[curr]}`), tpl);
49596
+ return encodeURI(url);
49597
+ };
49598
+ var generateWssUrl$1 = generateWssUrl;
49599
+
49600
+ const isValidKey = (key, object) => key in object;
49601
+ var isValidKey$1 = isValidKey;
49602
+
49603
+ /**
49604
+ * 解析参数
49605
+ * @param query
49606
+ * @returns
49607
+ */
49608
+ const parseQuery = (query = '') => query
49609
+ .replace('?', '')
49610
+ .split('&')
49611
+ .map((qs) => {
49612
+ const [k, v] = qs.split('=');
49613
+ return { [k]: decodeURIComponent(v) };
49614
+ })
49615
+ .reduce((result, curr) => ({ ...result, ...curr }), {});
49616
+ var parseQuery$1 = parseQuery;
49617
+
49618
+ const ws = (() => new Ws4Webview$1())();
49648
49619
  class HybridBridge {
49649
49620
  constructor() {
49650
- var _a, _b;
49651
49621
  /**
49652
49622
  * @deprecated 兼容旧逻辑
49653
49623
  */
49654
49624
  this._rsWeappBridgeMethodAdapter = {};
49655
- this._ws = new RobustWebSocket$1();
49656
- this._readyState = 'close';
49625
+ this._forceClose = false;
49657
49626
  /**
49658
49627
  * 进行jsbridge的初始化,即打通ws通道
49659
49628
  * @param timeout 超时计时
@@ -49676,7 +49645,7 @@
49676
49645
  sign: wssig,
49677
49646
  });
49678
49647
  try {
49679
- const result = await this._open(url);
49648
+ const result = await ws.connect({ url });
49680
49649
  if (result) {
49681
49650
  this.initBuiltInHandlers();
49682
49651
  res[0] = await this.waitReady(timeout);
@@ -49690,130 +49659,6 @@
49690
49659
  }
49691
49660
  return res;
49692
49661
  };
49693
- /**
49694
- * 内建的处理函数
49695
- */
49696
- this.initBuiltInHandlers = () => {
49697
- this._registerLifecycleHandler();
49698
- this._registerHeartbeatHandler();
49699
- this._registerMessageHandler();
49700
- this._registerBridgeRequestHandler();
49701
- this._registerReadyHandler();
49702
- };
49703
- /**
49704
- * 进行连接
49705
- * @param url ws链接
49706
- * @returns
49707
- */
49708
- this._open = (url) => new Promise((resolve) => {
49709
- const handleOpen = () => {
49710
- this._receivedMessage(WeappBridgeMessage.ready, () => resolve(true), true);
49711
- this._ws.once('timeout', () => resolve(false));
49712
- this.send(WeappBridgeMessage.isReady);
49713
- };
49714
- this._ws.once('open', handleOpen);
49715
- this._ws.open(url);
49716
- });
49717
- /**
49718
- * 注册生命周期回调
49719
- */
49720
- this._registerLifecycleHandler = () => {
49721
- this._ws.on('connecting', () => {
49722
- console.log('RsWeappBridge connecting');
49723
- this._readyState = 'connecting';
49724
- });
49725
- this._ws.on('open', () => {
49726
- console.log('RsWeappBridge open');
49727
- this._readyState = 'open';
49728
- });
49729
- this._ws.on('close', () => {
49730
- this._readyState = 'close';
49731
- });
49732
- this._ws.on('error', () => {
49733
- console.log('RsWeappBridge error');
49734
- this._readyState = 'close';
49735
- });
49736
- this._ws.on('timeout', () => {
49737
- console.log('RsWeappBridge timeout');
49738
- this._readyState = 'close';
49739
- });
49740
- };
49741
- /**
49742
- * 注册心跳的回调
49743
- * @deprecated 兼容老的容器SDK
49744
- */
49745
- this._registerHeartbeatHandler = () => {
49746
- // 收到ping时回复pong
49747
- this._receivedMessage(WeappBridgeMessage.ping, () => setTimeout(() => this._ws.send(WeappBridgeMessage.pong), 500));
49748
- // 收到pong时回复ping
49749
- this._receivedMessage(WeappBridgeMessage.pong, () => setTimeout(() => this._ws.send(WeappBridgeMessage.ping), 1000));
49750
- };
49751
- /**
49752
- * 当收到某消息时
49753
- * @param targetMessage 目标消息
49754
- * @param handler 回调
49755
- * @param once 是否一次性处理
49756
- */
49757
- this._receivedMessage = (targetMessage, handler, once) => {
49758
- const msgHandler = (evnet) => {
49759
- if (evnet.data === targetMessage) {
49760
- handler();
49761
- if (once) {
49762
- this._ws.off('message', msgHandler);
49763
- }
49764
- }
49765
- };
49766
- this._ws.on('message', msgHandler);
49767
- };
49768
- /**
49769
- * 注册准备就绪的回调
49770
- */
49771
- this._registerReadyHandler = () => {
49772
- // 收到就绪询问时回复已就绪
49773
- this._receivedMessage(WeappBridgeMessage.isReady, () => this._ws.send(WeappBridgeMessage.ready));
49774
- };
49775
- /**
49776
- * 发送消息
49777
- */
49778
- this.send = (data) => {
49779
- this._ws.send(data);
49780
- };
49781
- /**
49782
- * 等待响应
49783
- * @param timeout 超时计时
49784
- * @returns
49785
- */
49786
- this.waitReady = (timeout = 6000) => {
49787
- return new Promise((resolve, reject) => {
49788
- this._receivedMessage(WeappBridgeMessage.ready, () => resolve(true));
49789
- this.send(WeappBridgeMessage.isReady);
49790
- setTimeout(() => reject('HybridBridge 初始化超时~'), timeout);
49791
- });
49792
- };
49793
- this._parseMessage = (event) => {
49794
- try {
49795
- return JSON.parse(event.data);
49796
- }
49797
- catch (e) {
49798
- return event.data;
49799
- }
49800
- };
49801
- /**
49802
- * 注册消息投递回调
49803
- * @deprecated 兼容老方法
49804
- */
49805
- this._registerMessageHandler = () => {
49806
- this._ws.on('message', (event) => {
49807
- const message = this._parseMessage(event);
49808
- if (typeof message === 'object') {
49809
- const { id, command, data } = message;
49810
- if (command === 'message' &&
49811
- typeof this._rsWeappBridgeMethodAdapter.bindMessage === 'function') {
49812
- this._rsWeappBridgeMethodAdapter.bindMessage({ id, data });
49813
- }
49814
- }
49815
- });
49816
- };
49817
49662
  this.getStaticData = (callbackFunctionName) => {
49818
49663
  this.callAndBackfeed(JSON.stringify({
49819
49664
  actionUrl: 'common/staticData',
@@ -49860,52 +49705,134 @@
49860
49705
  callback(false);
49861
49706
  }
49862
49707
  };
49863
- /**
49864
- * 执行
49865
- * @param method
49866
- * @param args
49867
- * @returns
49868
- */
49869
- this.execute = async (method, args) => await this._invokeBridgeMethod(method, args);
49870
- /**
49871
- *
49872
- * @param method
49873
- * @param args
49874
- * @returns
49875
- * @deprecated 兼容老方法
49876
- */
49877
- this._invokeBridgeMethod = (method, args) => new Promise((success, fail) => {
49878
- const id = uuid();
49879
- const wsBridgeResponsehandler = (event) => {
49880
- const result = this._parseMessage(event);
49881
- if (typeof result === 'object') {
49882
- if (result.method === method && result.id === id) {
49883
- this._ws.off('message', wsBridgeResponsehandler);
49884
- result.error ? fail(result.error) : success(result.data);
49708
+ }
49709
+ get isReady() {
49710
+ return ws.readyState === WebSocketState.OPEN;
49711
+ }
49712
+ /**
49713
+ * 内建的处理函数
49714
+ */
49715
+ initBuiltInHandlers() {
49716
+ this._registerLifecycleHandler();
49717
+ this._registerHeartbeatHandler();
49718
+ this._registerMessageHandler();
49719
+ this._registerBridgeRequestHandler();
49720
+ this._registerReadyHandler();
49721
+ }
49722
+ /**
49723
+ * 注册生命周期回调
49724
+ */
49725
+ _registerLifecycleHandler() {
49726
+ ws.onOpen(() => console.log('RsWeappBridge open'));
49727
+ ws.onClose((res) => {
49728
+ console.log('RsWeappBridge close');
49729
+ if (res.code === 1000) {
49730
+ switch ((res.reason + '').toLowerCase()) {
49731
+ case WebSocketCloseOptions.lostHeartbeat.reason: {
49732
+ // 心跳丢失
49733
+ ws.reconnect();
49734
+ break;
49735
+ }
49736
+ // case WebSocketCloseOptions.normalClosure.reason: // 主动关闭
49737
+ case WebSocketCloseOptions.interrupted.reason: // 切换到后台,被微信杀掉,需要重连连接
49738
+ case WebSocketCloseOptions.streamEndEncountered.reason: // 服务端拒绝连接
49739
+ default: {
49740
+ if (!this._forceClose) {
49741
+ ws.reconnect();
49742
+ }
49743
+ break;
49885
49744
  }
49886
49745
  }
49887
- };
49888
- this._ws.on('message', wsBridgeResponsehandler);
49889
- this.send(JSON.stringify({ method, args, id }));
49746
+ }
49747
+ if (res.code !== 1000 && !this._forceClose) {
49748
+ ws.reconnect();
49749
+ }
49750
+ if (this._forceClose)
49751
+ this._forceClose = false;
49890
49752
  });
49891
- if (((_a = globalThis === null || globalThis === void 0 ? void 0 : globalThis.window) === null || _a === void 0 ? void 0 : _a.__Realsee_WxMini_HybridBridge__) instanceof HybridBridge) {
49892
- return (_b = globalThis === null || globalThis === void 0 ? void 0 : globalThis.window) === null || _b === void 0 ? void 0 : _b.__Realsee_WxMini_HybridBridge__;
49893
- }
49894
- if (globalThis === null || globalThis === void 0 ? void 0 : globalThis.window) {
49895
- globalThis.window.__Realsee_WxMini_HybridBridge__ = this;
49896
- this.initBuiltInHandlers();
49897
- }
49753
+ ws.onError((err) => console.error(err));
49898
49754
  }
49899
- get isReady() {
49900
- return this._readyState === 'open';
49755
+ /**
49756
+ * 注册心跳的回调
49757
+ * @deprecated 兼容老的容器SDK
49758
+ */
49759
+ _registerHeartbeatHandler() {
49760
+ // 收到ping时回复pong
49761
+ this._receivedMessage(WeappBridgeMessage.ping, () => ws.send(WeappBridgeMessage.pong));
49762
+ // 收到pong时回复ping
49763
+ this._receivedMessage(WeappBridgeMessage.pong, () => ws.send(WeappBridgeMessage.ping));
49764
+ }
49765
+ /**
49766
+ * 当收到某消息时
49767
+ * @param targetMessage 目标消息
49768
+ * @param handler 回调
49769
+ * @param once 是否一次性处理
49770
+ */
49771
+ _receivedMessage(targetMessage, handler, once) {
49772
+ const msgHandler = (msg) => {
49773
+ if (msg === targetMessage) {
49774
+ handler();
49775
+ if (once) {
49776
+ ws.offMessage(msgHandler);
49777
+ }
49778
+ }
49779
+ };
49780
+ ws.onMessage(msgHandler);
49781
+ }
49782
+ /**
49783
+ * 强制关闭ws
49784
+ * @param options
49785
+ */
49786
+ forceClose(options) {
49787
+ this._forceClose = true;
49788
+ ws.close(options);
49789
+ }
49790
+ /**
49791
+ * 注册准备就绪的回调
49792
+ */
49793
+ _registerReadyHandler() {
49794
+ // 收到就绪询问时回复已就绪
49795
+ this._receivedMessage(WeappBridgeMessage.isReady, () => ws.send(WeappBridgeMessage.ready));
49796
+ }
49797
+ /**
49798
+ * 发送消息
49799
+ */
49800
+ send(data) {
49801
+ ws.send(data);
49802
+ }
49803
+ /**
49804
+ * 等待响应
49805
+ * @param timeout 超时计时
49806
+ * @returns
49807
+ */
49808
+ waitReady(timeout = 6000) {
49809
+ return new Promise((resolve, reject) => {
49810
+ this._receivedMessage(WeappBridgeMessage.ready, () => resolve(true));
49811
+ this.send(WeappBridgeMessage.isReady);
49812
+ setTimeout(() => reject('HybridBridge 初始化超时~'), timeout);
49813
+ });
49814
+ }
49815
+ /**
49816
+ * 注册消息投递回调
49817
+ * @deprecated 兼容老方法
49818
+ */
49819
+ _registerMessageHandler() {
49820
+ ws.onMessage((message) => {
49821
+ if (typeof message === 'object') {
49822
+ const { id, command, data } = message;
49823
+ if (command === 'message' &&
49824
+ typeof this._rsWeappBridgeMethodAdapter.bindMessage === 'function') {
49825
+ this._rsWeappBridgeMethodAdapter.bindMessage({ id, data });
49826
+ }
49827
+ }
49828
+ });
49901
49829
  }
49902
49830
  /**
49903
49831
  * 注册bridge方法回调
49904
49832
  * @deprecated 兼容老方法
49905
49833
  */
49906
49834
  _registerBridgeRequestHandler() {
49907
- this._ws.on('message', async (event) => {
49908
- const request = this._parseMessage(event);
49835
+ ws.onMessage(async (request) => {
49909
49836
  if (typeof request === 'object') {
49910
49837
  const { id, method, args, command } = request;
49911
49838
  if (command === 'request') {
@@ -49958,25 +49885,22 @@
49958
49885
  type: 'callAndBackfeed',
49959
49886
  });
49960
49887
  let timer = null;
49961
- const callback = (event) => {
49962
- const message = this._parseMessage(event);
49963
- if (typeof message === 'object') {
49964
- const { id, result, errmsg, command } = message;
49965
- if (command === 'rsSchemeResponse' && id === reqId) {
49966
- this._ws.off('message', callback);
49967
- if (timer)
49968
- clearTimeout(timer);
49969
- if (errmsg) {
49970
- console.warn(errmsg);
49971
- backfeed(0);
49972
- }
49973
- else {
49974
- backfeed(result);
49975
- }
49888
+ const callback = (message) => {
49889
+ const { id, result, errmsg, command } = message;
49890
+ if (command === 'rsSchemeResponse' && id === reqId) {
49891
+ ws.offMessage(callback);
49892
+ if (timer)
49893
+ clearTimeout(timer);
49894
+ if (errmsg) {
49895
+ console.warn(errmsg);
49896
+ backfeed(0);
49897
+ }
49898
+ else {
49899
+ backfeed(result);
49976
49900
  }
49977
49901
  }
49978
49902
  };
49979
- this._ws.on('message', callback);
49903
+ ws.onMessage(callback);
49980
49904
  this.send(data);
49981
49905
  // 30s超时处理
49982
49906
  timer = globalThis.setTimeout(() => {
@@ -49997,22 +49921,19 @@
49997
49921
  command: 'rsSchemeRequest',
49998
49922
  type: 'callAndListen',
49999
49923
  });
50000
- const callback = (event) => {
50001
- const message = this._parseMessage(event);
50002
- if (typeof message === 'object') {
50003
- const { id, result, errmsg, command } = message;
50004
- if (command === 'rsSchemeResponse' && id === reqId) {
50005
- if (errmsg) {
50006
- console.warn(errmsg);
50007
- fn(false);
50008
- }
50009
- else {
50010
- fn(result);
50011
- }
49924
+ const callback = (message) => {
49925
+ const { id, result, errmsg, command } = message;
49926
+ if (command === 'rsSchemeResponse' && id === reqId) {
49927
+ if (errmsg) {
49928
+ console.warn(errmsg);
49929
+ fn(false);
49930
+ }
49931
+ else {
49932
+ fn(result);
50012
49933
  }
50013
49934
  }
50014
49935
  };
50015
- this._ws.on('message', callback);
49936
+ ws.onMessage(callback);
50016
49937
  this.send(data);
50017
49938
  }
50018
49939
  /**
@@ -50031,26 +49952,23 @@
50031
49952
  command: 'request',
50032
49953
  });
50033
49954
  let timer = null;
50034
- const callback = (event) => {
50035
- const message = this._parseMessage(event);
50036
- if (typeof message === 'object') {
50037
- const { id, result, error, command } = message;
50038
- if (command === 'response' &&
50039
- id === reqId &&
50040
- message.method === method) {
50041
- this._ws.off('message', callback);
50042
- if (timer)
50043
- clearTimeout(timer);
50044
- if (error) {
50045
- fail(error);
50046
- }
50047
- else {
50048
- success(result);
50049
- }
49955
+ const callback = (message) => {
49956
+ const { id, result, error, command } = message;
49957
+ if (command === 'response' &&
49958
+ id === reqId &&
49959
+ message.method === method) {
49960
+ ws.offMessage(callback);
49961
+ if (timer)
49962
+ clearTimeout(timer);
49963
+ if (error) {
49964
+ fail(error);
49965
+ }
49966
+ else {
49967
+ success(result);
50050
49968
  }
50051
49969
  }
50052
49970
  };
50053
- this._ws.on('message', callback);
49971
+ ws.onMessage(callback);
50054
49972
  this.send(data);
50055
49973
  // 30s超时处理
50056
49974
  timer = setTimeout(() => fail('method invoke timeout'), 30000);
@@ -50063,14 +49981,18 @@
50063
49981
  */
50064
49982
  class WxMiniprogramController extends BaseController {
50065
49983
  constructor() {
50066
- super(...arguments);
50067
- this._hybridBridge = new HybridBridge();
49984
+ super();
50068
49985
  this.initializing = false;
49986
+ Promise.resolve().then(async () => {
49987
+ try {
49988
+ await this.checkBridgeReady();
49989
+ }
49990
+ catch (error) {
49991
+ console.warn(error);
49992
+ }
49993
+ });
50069
49994
  }
50070
- get hybridBridge() {
50071
- return this._hybridBridge;
50072
- }
50073
- checkBridgeReady(timeout = 60000) {
49995
+ checkBridgeReady(timeout = 6000) {
50074
49996
  if (this.ready)
50075
49997
  return true;
50076
49998
  return new Promise(async (resolve, reject) => {
@@ -50087,11 +50009,12 @@
50087
50009
  }
50088
50010
  else {
50089
50011
  this.initializing = true;
50090
- const [res, error] = await this._hybridBridge.initialize(timeout);
50012
+ const hybridBridge = new HybridBridge();
50013
+ const [res, error] = await hybridBridge.initialize(timeout);
50091
50014
  if (!res || error) {
50092
50015
  throw error || new Error('HybridBridge 初始化失败~');
50093
50016
  }
50094
- this._hybridBridge.getStaticData(registerGlobalValue('HYBRID_BRIDGE_NAME', (data) => {
50017
+ hybridBridge === null || hybridBridge === void 0 ? void 0 : hybridBridge.getStaticData(registerGlobalValue('HYBRID_BRIDGE_NAME', (data) => {
50095
50018
  this.deviceInfo = (() => {
50096
50019
  try {
50097
50020
  return JSON.parse(data);
@@ -50100,13 +50023,14 @@
50100
50023
  return data;
50101
50024
  }
50102
50025
  })();
50026
+ this.hybridBridge = hybridBridge;
50027
+ this.initializing = false;
50028
+ return resolve(true);
50103
50029
  }));
50104
- this.initializing = false;
50105
- return resolve(true);
50106
50030
  }
50107
50031
  });
50108
50032
  }
50109
- async checkReady(timeout = 60000) {
50033
+ async checkReady(timeout = 6000) {
50110
50034
  const res = [null, null];
50111
50035
  if (this.ready) {
50112
50036
  res[0] = true;
@@ -50122,7 +50046,8 @@
50122
50046
  return res;
50123
50047
  }
50124
50048
  get ready() {
50125
- return !!this._hybridBridge.isReady;
50049
+ var _a;
50050
+ return !!((_a = this.hybridBridge) === null || _a === void 0 ? void 0 : _a.isReady);
50126
50051
  }
50127
50052
  get scheme() {
50128
50053
  var _a;
@@ -50164,7 +50089,7 @@
50164
50089
  resolve(res);
50165
50090
  }),
50166
50091
  });
50167
- this._hybridBridge.callAndBackfeed(msg);
50092
+ this.hybridBridge.callAndBackfeed(msg);
50168
50093
  });
50169
50094
  }
50170
50095
  async callAndListen(scheme, callback) {
@@ -50209,7 +50134,7 @@
50209
50134
  actionUrl,
50210
50135
  functionName: registerGlobalValue(actionUrl, _callback, false),
50211
50136
  });
50212
- this._hybridBridge.callAndListen(msg);
50137
+ this.hybridBridge.callAndListen(msg);
50213
50138
  });
50214
50139
  }
50215
50140
  async invoke(method, data) {
@@ -50219,7 +50144,7 @@
50219
50144
  throw readyError;
50220
50145
  }
50221
50146
  else {
50222
- return (await ((_b = (_a = this._hybridBridge).invoke) === null || _b === void 0 ? void 0 : _b.call(_a, method, data)));
50147
+ return (await ((_b = (_a = this.hybridBridge).invoke) === null || _b === void 0 ? void 0 : _b.call(_a, method, data)));
50223
50148
  }
50224
50149
  }
50225
50150
  }
@@ -50533,7 +50458,7 @@
50533
50458
  }
50534
50459
 
50535
50460
  const defaultOptions = {
50536
- timeout: 60000,
50461
+ timeout: 6000,
50537
50462
  };
50538
50463
  /**
50539
50464
  * 小程序 [web-view](https://developers.weixin.qq.com/miniprogram/dev/component/web-view.html) JSBridge。
@@ -50649,12 +50574,10 @@
50649
50574
  }
50650
50575
  const proxyCache = (url) => {
50651
50576
  if (this.webViewType === WebViewTypeEnum.WKWebview) {
50652
- return url
50653
- .replace(/https*:/, '')
50654
- .replace(/\/\//, 'realseeproxycache://');
50577
+ return url.replace(/https*:/, '').replace(/\/\//, 'realseeproxycache://');
50655
50578
  }
50656
50579
  else if (this.webViewType === WebViewTypeEnum.WebView) {
50657
- return (url + (url.indexOf('?') !== -1 ? '&' : '?') + 'realseeproxycache=1');
50580
+ return url + (url.indexOf('?') !== -1 ? '&' : '?') + 'realseeproxycache=1';
50658
50581
  }
50659
50582
  return url;
50660
50583
  };
@@ -50663,10 +50586,6 @@
50663
50586
  async invokeNativeApi(method, data) {
50664
50587
  return await this._controller.invoke(method, data);
50665
50588
  }
50666
- async execute(method, args) {
50667
- var _a, _b, _c;
50668
- return await ((_c = (_b = (_a = this._controller) === null || _a === void 0 ? void 0 : _a.hybridBridge) === null || _b === void 0 ? void 0 : _b.execute) === null || _c === void 0 ? void 0 : _c.call(_b, method, args));
50669
- }
50670
50589
  }
50671
50590
 
50672
50591
  /**
@@ -51584,6 +51503,13 @@
51584
51503
  return false;
51585
51504
  }
51586
51505
  const { force = false } = options;
51506
+ // 检测一下麦克风权限,hack
51507
+ if (this._options.rtc && this._options.jsBridge) {
51508
+ const res = yield this._options.jsBridge.detectMicro();
51509
+ if (!res) {
51510
+ return new Error('Failed to obtain microphone authorization');
51511
+ }
51512
+ }
51587
51513
  // 检查是否已有实例在使用
51588
51514
  const currentState = this.state;
51589
51515
  if (currentState === exports.LiveState.CONNECTING ||