|
@@ -0,0 +1,125 @@
|
|
|
|
|
+import { createWatchCore, PlayerEvents, ChatEvents} from '@polyv/live-watch-sdk';
|
|
|
|
|
+
|
|
|
|
|
+export default class Live {
|
|
|
|
|
+ watchCore = null;
|
|
|
|
|
+ channelId = "4247961";
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 回调方法:
|
|
|
|
|
+ * init_live 初始化完成
|
|
|
|
|
+ * chat_msg 监听聊天信息
|
|
|
|
|
+ * on_notice 监听公告
|
|
|
|
|
+ * @type {{}}
|
|
|
|
|
+ */
|
|
|
|
|
+ callbackMap = {}
|
|
|
|
|
+
|
|
|
|
|
+ constructor(channelId) {
|
|
|
|
|
+ this.channelId = channelId;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ async initWatchCore(callback) {
|
|
|
|
|
+ this.watchCore = createWatchCore({
|
|
|
|
|
+ channelId: this.channelId,
|
|
|
|
|
+ userInfo: {
|
|
|
|
|
+ userId: "e6soizn34h"
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ try {
|
|
|
|
|
+ await this.watchAuth()
|
|
|
|
|
+ await this.watchCore.setup();
|
|
|
|
|
+ // 注册互动sdk
|
|
|
|
|
+ await this.watchCore.interactReceive.setupIarCore();
|
|
|
|
|
+ // 连接
|
|
|
|
|
+ await this.watchCore.connect();
|
|
|
|
|
+ callback && callback(this.watchCore);
|
|
|
|
|
+ this.emit("init_live", this.watchCore);
|
|
|
|
|
+
|
|
|
|
|
+ this.getNotice();
|
|
|
|
|
+ } catch (err) {
|
|
|
|
|
+ console.error(err);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 观看授权
|
|
|
|
|
+ watchAuth() {
|
|
|
|
|
+ return new Promise( (resolve, reject) => {
|
|
|
|
|
+ this.watchCore.auth.verifyNoneAuth().then(result => {
|
|
|
|
|
+ if (result.success) {
|
|
|
|
|
+ resolve(result)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ reject(result)
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // 创建播放器
|
|
|
|
|
+ async createPlayer(container) {
|
|
|
|
|
+ this.watchCore.player.eventEmitter.on(PlayerEvents.PlayerInfoChange, (data) => {
|
|
|
|
|
+ const playerInfo = data.playerInfo;
|
|
|
|
|
+ console.log('播放状态', playerInfo.playStatus);
|
|
|
|
|
+ });
|
|
|
|
|
+ await this.watchCore.player.setupPlayer({
|
|
|
|
|
+ container,
|
|
|
|
|
+ barrageEnabled: false,
|
|
|
|
|
+ autoplay: true,
|
|
|
|
|
+ barrageShow: false,
|
|
|
|
|
+ showController: false,
|
|
|
|
|
+ // lowLatency: true, // 使用无延迟播放
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 获取聊天内容
|
|
|
|
|
+ async getChatContent(callback) {
|
|
|
|
|
+ const list = await this.watchCore.chat.getChatHistory();
|
|
|
|
|
+ callback && callback(list);
|
|
|
|
|
+ console.log("聊天内容", list);
|
|
|
|
|
+ this.listenChat();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 监听聊天信息
|
|
|
|
|
+ listenChat() {
|
|
|
|
|
+ this.watchCore.chat.eventEmitter.on(ChatEvents.ChatMessage, (data) => {
|
|
|
|
|
+ console.log("聊天信息", data);
|
|
|
|
|
+ this.emit("chat_msg", data);
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 发送消息
|
|
|
|
|
+ sendMessage(content) {
|
|
|
|
|
+ this.watchCore.chat.sendSpeakMsg({
|
|
|
|
|
+ content
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 获取公告
|
|
|
|
|
+ getNotice() {
|
|
|
|
|
+ const announcementSdk = this.watchCore.interactReceive.getAnnouncement();
|
|
|
|
|
+ announcementSdk.on(announcementSdk.events.BULLETIN, (msg) => {
|
|
|
|
|
+ this.emit("on_notice", msg);
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ on(key, callback) {
|
|
|
|
|
+ if (!this.callbackMap[key]) {
|
|
|
|
|
+ this.callbackMap[key] = new Set();
|
|
|
|
|
+ }
|
|
|
|
|
+ this.callbackMap[key].add(callback);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ emit(key, data) {
|
|
|
|
|
+ if (this.callbackMap[key]) {
|
|
|
|
|
+ this.callbackMap[key].forEach((callback) => {
|
|
|
|
|
+ callback(data);
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ off(key, callback) {
|
|
|
|
|
+ if (!this.callbackMap[key]) return;
|
|
|
|
|
+ this.callbackMap[key].delete(callback);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|