观看页核心
更新时间:2024-10-10 17:27:10
本文档主要提供观看页小程序 SDK 提供的核心方法 Api 说明。
在设计模式上,观看页核心实例是单例设计,所以 sdk 提供单独的方法来创建和销毁实例
一、创建观看页核心实例
Api 方法: createWatchCore(appConfig: AppConfig): PolyvWatchCore
参数说明:
- appConfig:配置选项,
AppConfig类型,必传,详细类型说明如下
| 参数名 | 说明 | 类型 | 必须 | 默认值 |
|---|---|---|---|---|
channelId |
频道号 | string |
是 | - |
domainInfo |
自定义域名 | Partial<DomainInfo> |
否 | - |
userInfo |
观众信息 | UserInfo |
否 | - |
vid |
回放 vid | string |
否 | - |
playbackOrigin |
回放类型 | PlaybackOrigin |
否 | - |
示例代码:
import { createWatchCore } from '@polyv/live-watch-miniprogram-sdk';
const watchCore = createWatchCore({
channelId: '频道号'
});
二、销毁观看页核心实例
Api 方法: destroyWatchCore(): void
注意:一般情况下不建议开发者主动去调用该方法,建议是随浏览器标签页关闭时,自动被浏览器销毁回收即可。
示例代码:
import { destroyWatchCore } from '@polyv/live-watch-miniprogram-sdk';
destroyWatchCore()
三、核心方法
3.1 设置授权 token
Api 方法: setXAuthToken(xAuthToken: string): void
参数说明:
- xAuthToken:授权 token,
string类型,必传
3.2 安装观看页核心
观看页核心方法,用于安装观看页核心并加载频道信息,安装完成后回调 Promise 及 PolyvWatchCoreEvents.WatchCoreSetuped 事件。
Api 方法: setup(): Promise<SetupResult>
返回值说明: Promise<SetupResult> 类型
示例:
import { createWatchCore } from '@polyv/live-watch-miniprogram-sdk';
const watchCore = createWatchCore({
channelId: '频道号'
});
watchCore.setup();
3.3 连接聊天室
观看页核心方法,用于在进入直播观看页之前连接聊天室,连接完成后回调 Promise 及 PolyvWatchCoreEvents.WatchCoreConnected 事件。
Api 方法: connect(): Promise<void>
示例:
await watchCore.connect();
console.log('连接成功,显示直播观看页');
