MessageEngine.js 4.0 KB
import Emiter from './Emiter';
import h5Sass from 'Sass';
import mcu from 'mcu';
import MessageTypes from 'MessageTypes';
import Loger from 'Loger';
import ConferApe from 'apes/ConferApe';
import ChatApe from 'apes/ChatApe';
import VideoChat from 'apes/VideoApe';
import DocApe from 'apes/DocApe';
import WhiteBoardApe from 'apes/WhiteBoardApe';
let loger = Loger.getLoger('MessageEngine');

export default class MessageEngine extends Emiter {
  constructor() {
    super();

    // 应用层会议信息
    this.confInfo = null;

    // Sass平台层
    this.h5Sass = h5Sass;
    this.h5Sass.on('*', (type, data) => this.emit(type, data));
    this.h5Sass.on(h5Sass.SUCCESS, this._h5SassSuccessHandler.bind(this));

    // 底层MCU消息层
    this.mcu = mcu;
    this.mcu.on('*', (type, data) => this.emit(type, data));
    this.mcu.on(MessageTypes.CONFERENCE_JOIN_SUCCESS, this._conferenceJoinSuccessHandler.bind(this));

    // 注册所有应用Ape
    this.conferApe = new ConferApe();
    this.conferApe.on('*', (type, data) => this.emit(type, data));
    this.conferApe.on(MessageTypes.DOC_SWITCH, this.docSwitchHandler.bind(this));

    this.chat_ape = new ChatApe();
    this.chat_ape.on('*', (type, data) => this.emit(type, data));

    this.video_ape = new VideoChat();
    this.video_ape.on('*', (type, data) => this.emit(type, data));

    this.doc_ape = new DocApe();
    this.doc_ape.on('*', (type, data) => this.emit(type, data));
    this.doc_ape.on(MessageTypes.DOC_UPDATE, this.docUpdateHandler.bind(this));

    this.doc_ape.on(MessageTypes.DOC_DEL, this.docDeleteHandler.bind(this));

    this.wb_ape = new WhiteBoardApe();
    this.wb_ape.on('*', (type, data) => this.emit(type, data));
    this.wb_ape.on(MessageTypes.ANNO_UPDATE, this.annoUpdateHandler.bind(this));

    this.on(MessageTypes.DOC_SHOW, this.docShowHandler.bind(this));
    //this.say = this.say.bind(this)
    //this.hello = this.hello.bind(this)
    this.init=this._init;
  }
  _init (id) {
    console.log("Engine init "+id);
  }
  // 白板笔记更新
  annoUpdateHandler(annoInfo) {
    const activeDocId = this.conferApe.activeDocId;
    const docItem = this.doc_ape.docList[activeDocId];
    if (docItem && annoInfo.id == docItem.wbid) {
      this.emit(MessageTypes.DOC_ANNO, annoInfo);
    }
  }

  // 文档变更-笔记处理
  docShowHandler(docItem) {
    loger.log('Doc Show ->' + docItem.id + '|' + docItem.curPageNo);

    const annoInfo = this.wb_ape.annoInfos[docItem.wbid];
    if (annoInfo) {
      this.emit(MessageTypes.DOC_ANNO, annoInfo);
    } else {
      this.emit(MessageTypes.DOC_ANNO);

    }

  }

  // 文档切换
  docSwitchHandler() {
    const activeDocId = this.conferApe.activeDocId;
    loger.log('Switch Doc Active -> ' + activeDocId);
    const docItem = this.doc_ape.docList[activeDocId];
    if (docItem) {
      this.emit(MessageTypes.DOC_SHOW, docItem);
    }
  }

  // 文档变更
  docUpdateHandler(docItem) {
    loger.log('Doc UpdateId ->' + docItem.id + '| activeDocId ->' + this.conferApe.activeDocId);
    if (docItem.id == this.conferApe.activeDocId) {
      this.emit(MessageTypes.DOC_SHOW, docItem);
    }
  }

  //文档删除
    docDeleteHandler(docItem){
        if (docItem.id == this.conferApe.activeDocId) {
            this.emit(MessageTypes.DOC_DEL, docItem);
        }
    }


  // MCU 会议成功
  _conferenceJoinSuccessHandler() {
    this.h5Sass.getClassDetail();
  }

  // 通过SASS平台验证
  _h5SassSuccessHandler() {
    loger.log('加入底层MCU会议.');
    this.mcu.joinMCU(this.confInfo);
  }

  // 进入会议
  joinClass(_confInfo) {
    this.confInfo = _confInfo;
    this.h5Sass.sassChecking(_confInfo);
  }

  // 离开会议
  leaveClass() {
    this.mcu.leaveMCU();
  }

  // 参会处理
  conferenceHandler(msg_type) {
    var msg = {
      type: msg_type,
      data: null
    };
    this.emit(msg.type, msg);
  }

  // 发送聊天消息
  sendChatMsg(to, msg) {
    if (this.chat_ape) {
      this.chat_ape.sendChatMsg(to, msg);
    }
  }
}

MessageEngine.MessageTypes = MessageTypes;
MessageEngine.data={
  aa:"aa",
  bb:11,
  cc:"cccc"
};