LiveRoom.ets 3.69 KB
import { ChatroomDestroyType,
  ChatroomJoinedInfo,
  ChatroomStatusListener,
  EngineError,
  IMEngine, Message, ReceivedInfo,
  TextMessage} from '@rongcloud/imlib';
import { Logger } from 'wdKit/Index';
import { LiveRoomItemBean } from 'wdBean/Index';

import { LiveRoomBaseInfo } from './LiveRoomBaseInfo'

import { JSON } from '@kit.ArkTS';
import { LiveMessageIsHistoryMessage, LiveMessageOptType } from 'wdBean/src/main/ets/bean/live/LiveRoomBean';

const TAG = "LiveRoomManager"

export class LiveRoom extends ChatroomStatusListener {

  connectRoomBaseInfo?: LiveRoomBaseInfo

  /// 回调
  onJoined?: (room: LiveRoom) => void
  onJoinFailed?: (room: LiveRoom, code: number) => void
  onExited?: (room: LiveRoom) => void
  ///用于展示的历史消息,包含聊天室和直播间
  onHistoryMessage?: (liveRoomItemBean: LiveRoomItemBean) => void
  ///用于管理直播间状态的消息,直播垫片,直播结束,上墙/取消上墙,置顶/取消置顶等
  onLiveMessage?: (liveRoomItemBean: LiveRoomItemBean) => void

  constructor(baseInfo?: LiveRoomBaseInfo) {
    super()
    this.connectRoomBaseInfo = baseInfo
  }

  enterRoom() {
    let roomId = this.connectRoomBaseInfo?.roomID
    let msgCount = 0;

    Logger.debug(TAG, `will enterRoom roomId: ${roomId}`);
    IMEngine.getInstance().joinChatroom(roomId, msgCount).then(result => {
      if (EngineError.Success !== result.code) {
        // 加入聊天室失败
        Logger.error(TAG, 'onChatroomJoinFailed roomId: ' + roomId + "  code: " + result.code);
        return;
      }
      if (!result.data) {
        // 聊天室加入信息失败
        return;
      }
      let joinedInfo = result.data as ChatroomJoinedInfo;
    });
  }

  exitRoom() {
    let roomId = this.connectRoomBaseInfo?.roomID

    IMEngine.getInstance().quitChatroom(roomId).then(result => {
      if (EngineError.Success !== result.code) {
        // 退出聊天室失败
        return;
      }
    });
  }

  // ---- ChatroomStatusListener
  onChatroomJoining(roomId: string): void {
    Logger.debug(TAG, `onChatroomJoining roomId: ${roomId}`);
  }

  onChatroomJoined(roomId: string, info: ChatroomJoinedInfo): void {
    Logger.debug(TAG, `onChatroomJoined roomId` + roomId + " " +  JSON.stringify(info));
    if (this.onJoined) {
      this.onJoined(this)
    }
  }

  onChatroomJoinFailed(roomId: string, code: EngineError): void {
    Logger.error(TAG, 'onChatroomJoinFailed roomId: ' + roomId + "  code: " + code);
    if (this.onJoinFailed) {
      this.onJoinFailed(this, code)
    }
  }

  onChatroomQuited(roomId: string): void {
    Logger.warn(TAG, 'onChatroomQuited roomId: ' + roomId);
    if (this.onExited) {
      this.onExited(this)
    }
  }

  onChatroomDestroyed(roomId: string, type: ChatroomDestroyType): void {
    Logger.debug(TAG, 'onChatroomDestroyed roomId: ' + roomId + "  type: " + type);
  }

  // --- on message
  onMessage(message: Message, info: ReceivedInfo) {

    Logger.debug(TAG, 'onMessage message: ' + JSON.stringify(message));

    if (message.objectName != "RC:TxtMsg") {
      return
    }
    let textMsg = message.content as TextMessage
    let liveRoomItemBean = JSON.parse(textMsg.content) as LiveRoomItemBean

    if (liveRoomItemBean == undefined) {
      return
    }

    let optionType = liveRoomItemBean.optionType != undefined ? liveRoomItemBean.optionType : liveRoomItemBean.dataType

    if (LiveMessageIsHistoryMessage(optionType)) {
      liveRoomItemBean.customFormIM = true
      if (this.onHistoryMessage) {
        this.onHistoryMessage(liveRoomItemBean)
      }
      return
    }
    if (this.onLiveMessage) {
      // liveRoomItemBean.pv = Number(liveRoomItemBean.pv) + 1000000 + ""
      this.onLiveMessage(liveRoomItemBean)
    }

  
  }
}