Live.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { createWatchCore, PlayerEvents, ChatEvents} from '@polyv/live-watch-sdk';
  2. export default class Live {
  3. watchCore = null;
  4. channelId = "4247961";
  5. /**
  6. * 回调方法:
  7. * init_live 初始化完成
  8. * chat_msg 监听聊天信息
  9. * on_notice 监听公告
  10. * @type {{}}
  11. */
  12. callbackMap = {}
  13. constructor(channelId) {
  14. this.channelId = channelId;
  15. }
  16. async initWatchCore(callback) {
  17. this.watchCore = createWatchCore({
  18. channelId: this.channelId,
  19. userInfo: {
  20. userId: "e6soizn34h"
  21. }
  22. })
  23. try {
  24. await this.watchAuth()
  25. await this.watchCore.setup();
  26. // 注册互动sdk
  27. await this.watchCore.interactReceive.setupIarCore();
  28. // 连接
  29. await this.watchCore.connect();
  30. callback && callback(this.watchCore);
  31. this.emit("init_live", this.watchCore);
  32. this.getNotice();
  33. } catch (err) {
  34. console.error(err);
  35. }
  36. }
  37. // 观看授权
  38. watchAuth() {
  39. return new Promise( (resolve, reject) => {
  40. this.watchCore.auth.verifyNoneAuth().then(result => {
  41. if (result.success) {
  42. resolve(result)
  43. } else {
  44. reject(result)
  45. }
  46. });
  47. })
  48. }
  49. // 创建播放器
  50. async createPlayer(container) {
  51. this.watchCore.player.eventEmitter.on(PlayerEvents.PlayerInfoChange, (data) => {
  52. const playerInfo = data.playerInfo;
  53. console.log('播放状态', playerInfo.playStatus);
  54. });
  55. await this.watchCore.player.setupPlayer({
  56. container,
  57. barrageEnabled: false,
  58. autoplay: true,
  59. barrageShow: false,
  60. showController: false,
  61. // lowLatency: true, // 使用无延迟播放
  62. });
  63. }
  64. // 获取聊天内容
  65. async getChatContent(callback) {
  66. const list = await this.watchCore.chat.getChatHistory();
  67. callback && callback(list);
  68. console.log("聊天内容", list);
  69. this.listenChat();
  70. }
  71. // 监听聊天信息
  72. listenChat() {
  73. this.watchCore.chat.eventEmitter.on(ChatEvents.ChatMessage, (data) => {
  74. console.log("聊天信息", data);
  75. this.emit("chat_msg", data);
  76. });
  77. }
  78. // 发送消息
  79. sendMessage(content) {
  80. this.watchCore.chat.sendSpeakMsg({
  81. content
  82. })
  83. }
  84. // 获取公告
  85. getNotice() {
  86. const announcementSdk = this.watchCore.interactReceive.getAnnouncement();
  87. announcementSdk.on(announcementSdk.events.BULLETIN, (msg) => {
  88. this.emit("on_notice", msg);
  89. });
  90. }
  91. on(key, callback) {
  92. if (!this.callbackMap[key]) {
  93. this.callbackMap[key] = new Set();
  94. }
  95. this.callbackMap[key].add(callback);
  96. }
  97. emit(key, data) {
  98. if (this.callbackMap[key]) {
  99. this.callbackMap[key].forEach((callback) => {
  100. callback(data);
  101. })
  102. }
  103. }
  104. off(key, callback) {
  105. if (!this.callbackMap[key]) return;
  106. this.callbackMap[key].delete(callback);
  107. }
  108. }