EngineEntrance.js 17.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
require('es6-promise').polyfill();
require('whatwg-fetch');
require('jquery-touchswipe');
require('string.fromcodepoint');

import Emiter from './Emiter';
import Sass 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 VideoApe from 'apes/VideoApe';
import DocApe from 'apes/DocApe';
import WhiteBoardApe from 'apes/WhiteBoardApe';
import EngineUtils from "EngineUtils";
import GlobalConfig from 'GlobalConfig';


let loger = Loger.getLoger('MessageEntrance');
let _sdkInfo={"version":"v.1.0.1","author":"www.3mang.com"};

//APE
let _sass;
let _mcu ;
let _confer_ape;
let _chat_ape;
let _video_ape;
let _doc_ape;
let _whiteboard_ape;

//初始化成功回调函数
let _initSuccessCallBackFun;

//加入会议成功回调函数
let _joinClassSuccessCallBackFun;

//监听mcu所有错误异常回调函数
let _mcuErrorCallBackFun;


//MCUClient 外部实例化主类
export default class MessageEntrance extends Emiter {
  constructor() {
    super();
    //sdk 信息
    this.sdkInfo=_sdkInfo;
    loger.log(this.sdkInfo);

    //初始化状态
    GlobalConfig.setCurrentStatus(GlobalConfig.statusCode_0);

    this.on(MessageTypes.MCU_ERROR,this._mcuErrorHandler.bind(this));
    this.on(MessageTypes.DOC_SHOW, this.docShowHandler.bind(this));

    // Sass平台层
    _sass = Sass;
    _sass.on('*', (type, data) => this._emit(type, data));
    _sass.on(_sass.SUCCESS, this._sassVerifySuccessHandler.bind(this));
    _sass.on(_sass.CLASS_INIT_SUCCESS, this._sassInitSuccessHandler.bind(this));
    _sass.on(_sass.CLASS_GET_CLASS_DETAIL, this._h5SassGetClassDetailSuccessHandler.bind(this));
    _sass.on(_sass.CLASS_GET_MEETING_PARAM,this._h5SassGetMeetingParaSuccessHandler);

    // 底层MCU消息层
    _mcu = Mcu;
    _mcu.on('*', (type, data) => this._emit(type, data));
    _mcu.on(MessageTypes.CLASS_JOIN_SUCCESS, this._joinClassSuccessHandler.bind(this));


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

    _chat_ape = new ChatApe();
    _chat_ape.on('*', (type, data) => this._emit(type, data));

    _video_ape = new VideoApe();
    _video_ape.on('*', (type, data) => this._emit(type, data));

    _doc_ape = new DocApe();
    _doc_ape.on('*', (type, data) => this._emit(type, data));
    _doc_ape.on(MessageTypes.DOC_UPDATE, this.docUpdateHandler.bind(this));
    _doc_ape.on(MessageTypes.DOC_DELETE, this.docDeleteHandler.bind(this));


    _whiteboard_ape = new WhiteBoardApe();
    _whiteboard_ape.on('*', (type, data) => this._emit(type, data));
    _whiteboard_ape.on(MessageTypes.WHITEBOARD_ANNOTATION_UPDATE, this.annoUpdateHandler.bind(this));


    //公开外部调用的方法
    //class
    this.init=this._init;
    this.joinClass=this._joinClass;
    this.leaveClass=this._leaveClass;
    this.mcuClientStatus=this._mcuClientStatus;

    //chatApe
    this.sendChatMsg=this._sendChatMsg;

    //videoApe
    this.getPlayVideoPath=this._getPlayVideoPath;
    this.getPublishVideoPath=this._getPublishVideoPath;
    this.publishVideo=this._publishVideo;
    this.stopPublishVideo=this._stopPublishVideo;


    //whiteBoradApe
    this.sendInsertAnnotaion=this._sendInsertAnnotaion;
    //this.sendUpdaterAnnotaion=this._sendUpdaterAnnotaion;//暂时关闭更新接口
    this.sendDeleteAnnotaion=this._sendDeleteAnnotaion;
    this.sendDeleteAllAnnotation=this._sendDeleteAllAnnotation;

    //DocApe
    this.sendDocumentUpload= this._sendDocumentUpload;//上传文档
    this.sendDocumentSwitch=  this._sendDocumentSwitch; //切换文档
    this.sendDocumentDelete= this. _sendDocumentDelete;//删除文档
    this.sendDocumentDeleteAll= this._documentDeleteAll;//删除所有文档
    this.sendDocumentCommand= this._sendDocumentCommand;//操作文档(翻页、缩放、滚动...)

    //debug
    //this.setDebugData=this._setDebugData;
  }


  //mcu异常监听
  _mcuErrorHandler(_data){
    if(_mcuErrorCallBackFun){
      let errorMessage={"code":_data,"reson":""};
      loger.log("MCU_ERROR",errorMessage);
      _mcuErrorCallBackFun(errorMessage);
    }
  }

  //获取当前的状态
  _mcuClientStatus(){
    return GlobalConfig.getCurrentStatus();
  }

  //初始化
  _init(_param,_onSuccess,_mcuErrorCallBack){
    _initSuccessCallBackFun=_onSuccess;
   // _initFailureCallBackFun=_onFailure;
    _mcuErrorCallBackFun=_mcuErrorCallBack;
    //{"meetingNumber":"1653304953","portal":"112.126.80.182:80","userRole":"normal","userId":0}
    //判断传入的参数是否存在
    if(_param==null||EngineUtils.isEmptyObject(_param)||_onSuccess==null||_mcuErrorCallBack==null){
      loger.log('init初始化失败,参数错误');
      this._emit(MessageTypes.MCU_ERROR,MessageTypes.ERR_CLASS_INIT_PAEAM);
      return ;
    }
    //判断必要的参数字段值
    if(_param.meetingNumber==null||isNaN(_param.meetingNumber)||_param.portal==null||_param.portal==""){
      loger.log('init初始化失败',_param);
      this._emit(MessageTypes.MCU_ERROR,MessageTypes.ERR_CLASS_INIT_PAEAM);
      return ;
    }
    loger.log('init',_param);
    //保存参数
    GlobalConfig.confId=_param.meetingNumber;
    GlobalConfig.portal=_param.portal;
    GlobalConfig.userRole=_param.userRole||"normal";
    GlobalConfig.userId=_param.userId||"0";

    //获取课堂校验信息
    if(_sass){
      _sass.getJoinParams(GlobalConfig.getConfInfo());
    }
  }

  //外部请求加入会议
  _joinClass(_param,_onSuccess){
    _joinClassSuccessCallBackFun=_onSuccess;
    //{"userName":"名字","password":""}
    if(_param==null||EngineUtils.isEmptyObject(_param)||_onSuccess==null){
      this._emit(MessageTypes.MCU_ERROR,MessageTypes.ERR_CLASS_JOIN_PARAM);
      loger.log('不能进入会议,传递的参数不对.',_param);
      return ;
    }
    //判断userName
    if(_param.userName==null||_param.userName==""){
      loger.log('不能进入会议,传递的参数不对.名字不能为空');
      this._emit(MessageTypes.MCU_ERROR,MessageTypes.ERR_CLASS_JOIN_PARAM);
      return ;
    }

    GlobalConfig.userName=_param.userName;
    GlobalConfig.password=_param.password||"";

    //开始校验
    if(_sass){
      _sass.passwordAndMd5Checking(GlobalConfig.getConfInfo());
    }
  }
  // 离开会议
  _leaveClass() {
    if(_confer_ape){
      _confer_ape.leaveClass();
    }
    if(_mcu){
      _mcu.leaveMCU();
      GlobalConfig.setCurrentStatus(GlobalConfig.statusCode_3);
    }
  }

  // 用meetingNumber向SASS平台获取入会验证信息成功
  _sassInitSuccessHandler(_data) {
    //{"siteId":"h5test","passwordRequired":true,"md5":"de399d5540b3da2fbc1eb0a770d4fd66","code":0,"msType":1}

    //储存数据
    GlobalConfig.md5=_data.md5||"";//这个暂时用假数据,后台接口写完就有数据了
    GlobalConfig.msType=_data.msType||1;
    GlobalConfig.siteId=_data.siteId||"";
    GlobalConfig.classType=_data.classType||"";

    //host默认需要密码,Sass服务器只判断学生是否需要密码,没有判断老师的
    if(GlobalConfig.userRole=="host"){
        GlobalConfig.passwordRequired=true;
    }else {
        GlobalConfig.passwordRequired=_data.passwordRequired||false;
    }

    loger.log('向SASS平台获取入会验证信息成功.');

    //this._emit(MessageTypes.CLASS_INIT_SUCCESS,initSuccessCallBackData);

    //设置当前的会议状态
    GlobalConfig.setCurrentStatus(GlobalConfig.statusCode_1);

    if(_initSuccessCallBackFun){
      //返回给客户端初始化成功的数据
      let initSuccessCallBackData={};
      initSuccessCallBackData.siteId=GlobalConfig.siteId;
      initSuccessCallBackData.meetingNumber=GlobalConfig.confId;
      initSuccessCallBackData.userRole=GlobalConfig.userRole;
      initSuccessCallBackData.userId=GlobalConfig.userId;
      initSuccessCallBackData.passwordRequired=GlobalConfig.passwordRequired;
      initSuccessCallBackData.classType=GlobalConfig.classType;

      _initSuccessCallBackFun(initSuccessCallBackData);
    }
  }

  //// 用meetingNumber向SASS平台获取入会验证信息失败
  //_sassInitFailedHandler(_data) {
  //  loger.log('向SASS平台获取入会验证信息失败.');
  //  //this._emit(MessageTypes.CLASS_INIT_FAILED);
  //  let callBackObj={"code":_data,"message":""};
  //  if(_initFailureCallBackFun){
  //    _initFailureCallBackFun(callBackObj);
  //  }
  //}

  //使用固定的假数据
  _setDebugData(_data){
    loger.log("setDebugData "+_data);
    //GlobalConfig.setDebugData(_data);
  }

  // 通过SASS平台验证
  _sassVerifySuccessHandler(_data) {

    //返回值
   /* flag	数值型	无		True:成功
    Flag:失败
    h5_mcu_list	字符串			H5muc列表
    maxVideoChannels	数值型			最大视频路数
    maxAudioChannels	数值型			最大音频路数
    h5Module	数值型			H5开关
    ms	字符串			Ms列表
    mcu	字符串			Mcu列表
    rs	字符串			Rs列表
    doc	字符串			Doc列表*/

    GlobalConfig.doc=_data.doc;
    GlobalConfig.h5_mcu_list=_data.h5_mcu_list;
    GlobalConfig.h5Module=_data.h5Module;
    GlobalConfig.maxAudioChannels=_data.maxAudioChannels;
    GlobalConfig.maxVideoChannels=_data.maxVideoChannels;
    GlobalConfig.mcu=_data.mcu;
    GlobalConfig.ms=_data.ms;
    GlobalConfig.record=_data.record;
    GlobalConfig.rs=_data.rs;


    loger.log('加入底层MCU会议.');
    if(_mcu){
      _mcu.joinMCU(GlobalConfig.getConfInfo());
    }
  }

  //获取会议所有参数
  _h5SassGetClassDetailSuccessHandler(_data){
    loger.log('获取getClassDetail完成.');
    GlobalConfig.classDetail=_data;
    loger.log(GlobalConfig.classDetail);
    this._emit(MessageTypes.CLASS_SHOW_DETAIL, _data);
  }

  //获取会议所有参数
  _h5SassGetMeetingParaSuccessHandler(_data){
    loger.log('获取getMeetingParam完成.');
    GlobalConfig.meetingParam=_data;
   // loger.log(GlobalConfig.meetingParam);
  }

  // MCU 会议成功
  _joinClassSuccessHandler(_data) {
    loger.log('MCU 会议成功.');
    GlobalConfig.setCurrentStatus(GlobalConfig.statusCode_2);
    if(_sass){
      _sass.getClassDetail();//会议信息
      _sass.getMeetingParam();//会议参数大全
    }

    //储存数据

    GlobalConfig.MCUServerIP=_data.MCUServerIP;
    GlobalConfig.MCUServerPort=_data.MCUServerPort;
    GlobalConfig.classRole=_data.classRole;
    GlobalConfig.confId=_data.confId;
    GlobalConfig.h5Module=_data.h5Module;
    GlobalConfig.isHost=_data.isHost;
    GlobalConfig.maxAudioChannels=_data.maxAudioChannels;
    GlobalConfig.maxVideoChannels=_data.maxVideoChannels;
    GlobalConfig.maxMediaChannels=_data.maxMediaChannels;
    GlobalConfig.mcuConfInfo=_data.mcuConfInfo;
    GlobalConfig.mcuDelay=_data.mcuDelay;
    GlobalConfig.md5=_data.md5;
    GlobalConfig.msType=_data.msType;
    GlobalConfig.nodeId=_data.nodeId;
    GlobalConfig.password=_data.password;
    //GlobalConfig.passwordRequired  老师的默认是true
    GlobalConfig.portal=_data.portal;
    GlobalConfig.role=_data.role;
    GlobalConfig.siteId=_data.siteId;
    GlobalConfig.topNodeID=_data.topNodeID;
    GlobalConfig.userId=_data.userId;
    GlobalConfig.userName=_data.userName;
    GlobalConfig.userRole=_data.userRole;
    GlobalConfig.userType=_data.userType;

    //返回给客户数据
    if(_joinClassSuccessCallBackFun){
      //返回给客户端初始化成功的数据
      let initSuccessCallBackData={};

      //GlobalConfig.MCUServerIP=_data.MCUServerIP;
     // GlobalConfig.MCUServerPort=_data.MCUServerPort;

      initSuccessCallBackData.doc=GlobalConfig.doc;//文档服务器地址
      initSuccessCallBackData.classRole=GlobalConfig.classRole;
      initSuccessCallBackData.confId=GlobalConfig.confId;
      initSuccessCallBackData.h5Module=GlobalConfig.h5Module;
      initSuccessCallBackData.isHost=GlobalConfig.isHost;
      initSuccessCallBackData.maxAudioChannels=GlobalConfig.maxAudioChannels;
      initSuccessCallBackData.maxVideoChannels=GlobalConfig.maxVideoChannels;
      initSuccessCallBackData.maxMediaChannels=GlobalConfig.maxMediaChannels;
     // GlobalConfig.mcuConfInfo=_data.mcuConfInfo;
      initSuccessCallBackData.mcuDelay=GlobalConfig.mcuDelay;
      //GlobalConfig.md5=_data.md5;
      initSuccessCallBackData.msType=GlobalConfig.msType;
      initSuccessCallBackData.nodeId=GlobalConfig.nodeId;
      initSuccessCallBackData.password=GlobalConfig.password;
      initSuccessCallBackData.passwordRequired=GlobalConfig.passwordRequired;//  老师的默认是true
      //GlobalConfig.passwordRequired  老师的默认是true
      //GlobalConfig.portal=_data.portal;
      initSuccessCallBackData.role=GlobalConfig.role;
      initSuccessCallBackData.siteId=GlobalConfig.siteId;
      initSuccessCallBackData.topNodeID=GlobalConfig.topNodeID;
      initSuccessCallBackData.userId=GlobalConfig.userId;
      initSuccessCallBackData.userName=GlobalConfig.userName;
      initSuccessCallBackData.userRole=GlobalConfig.userRole;
      initSuccessCallBackData.userType=GlobalConfig.userType;

      initSuccessCallBackData.siteId=GlobalConfig.siteId;
      initSuccessCallBackData.meetingNumber=GlobalConfig.confId;
      initSuccessCallBackData.userRole=GlobalConfig.userRole;
      initSuccessCallBackData.userId=GlobalConfig.userId;
      initSuccessCallBackData.passwordRequired=GlobalConfig.passwordRequired;
      initSuccessCallBackData.classType=GlobalConfig.classType;
      loger.log('加入会议成功',initSuccessCallBackData);
      _joinClassSuccessCallBackFun(initSuccessCallBackData);
    }
  }
  //// MCU 会议失败
  //_joinClassFailureHandler(_data){
  //  GlobalConfig.setCurrentStatus(GlobalConfig.statusCode_1);
  //  loger.log("_joinClassFailureHandler",_data);
  //  if(_joinClassFailureCallBackFun){
  //    let callBackObj={"code":_data,"message":""};
  //    _joinClassFailureCallBackFun(callBackObj);
  //  }
  //}

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

  // 发送聊天消息
  _sendChatMsg(_messageInfo) {
    if(_messageInfo===null||EngineUtils.isEmptyObject(_messageInfo)){
      loger.log('sendChatMsg 传递的参数不对',_messageInfo);
      return ;
    }
    if (_chat_ape) {
      _chat_ape.sendChatMsg(_messageInfo);
    }
  }

  //VidoeApe
  _getPlayVideoPath(_param){
    if(_video_ape){
      return _video_ape.getPlayVideoPath(_param);
    }
  }

  _getPublishVideoPath(_param){
    if(_video_ape){
      return _video_ape.getPublishVideoPath(_param);
    }
  }

  _publishVideo(_param){
    if(_video_ape){
      return _video_ape.publishVideo(_param);
    }
  }

  _stopPublishVideo(_param){
    if(_video_ape){
      return _video_ape.stopPublishVideo(_param);
    }
  }

  //WhiteBoardApe
  // 添加标注,发送信息
  _sendInsertAnnotaion(_param){
    if(_whiteboard_ape){
      _whiteboard_ape.sendInsetAnnotaion(_param);
    }
  }
  //更新标注,发送信息
  _sendUpdaterAnnotaion(_param){
    if(_whiteboard_ape){
      _whiteboard_ape.sendUpdaterAnnotaion(_param);
    }
  }
  //删除标注,发送信息
  _sendDeleteAnnotaion(_param){
    if(_whiteboard_ape){
      _whiteboard_ape.sendDeleteAnnotaion(_param);
    }
  }
//删除所有标注
  _sendDeleteAllAnnotation(_param){
    if(_whiteboard_ape){
      _whiteboard_ape.sendDeleteAllAnnotation(_param);
    }
  }

  //DocApe
  //上传文档
  _sendDocumentUpload(_param){
    if(_doc_ape){
      _doc_ape.documentUpload(_param);
    }
  }
  //切换文档
  _sendDocumentSwitch(_param){
    if(_doc_ape){
      _doc_ape.documentSwitch(_param);
    }
  }
  //删除文档
  _sendDocumentDelete(_param){
    if(_doc_ape){
      _doc_ape.documentDelete(_param);
    }
  }
  //删除所有文档
  _documentDeleteAll(_param){
    if(_doc_ape){
      _doc_ape.documentDeleteAll(_param);
    }
  }

  //操作文档(翻页、缩放、滚动...)
  _sendDocumentCommand(_param){
    if(_doc_ape){
      _doc_ape.documentCommand(_param);
    }
  }

  // 白板笔记更新(svg)
  annoUpdateHandler(annoInfo) {
    const activeDocId = _confer_ape.activeDocId;
    const docItem = _doc_ape.docList[activeDocId];
    if (docItem && annoInfo.id == docItem.wbid) {
      this._emit(MessageTypes.DOC_ANNOTATION, annoInfo);
    }
  }

  // 文档变更-笔记处理
  docShowHandler(docItem) {
    loger.log('Doc Show ->' + docItem.id + '|' + docItem.curPageNo);
    const annoInfo = _whiteboard_ape.annoInfos[docItem.wbid];
    if (annoInfo) {
      this._emit(MessageTypes.DOC_ANNOTATION, annoInfo);
    } else {
      this._emit(MessageTypes.DOC_ANNOTATION);

    }
  }

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

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

  //文档删除
    docDeleteHandler(docItem){
        if (docItem.id == _confer_ape.activeDocId) {
            this._emit(MessageTypes.DOC_DELETE, docItem);
        }
    }
}