ConferApe.js 5.4 KB
// //////////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2016-present  All Rights Reserved.
//  Licensed under the Apache License, Version 2.0 (the "License");
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Github Home: https://github.com/AlexWang1987
//  Author: AlexWang
//  Date: 2016-08-22 09:57:12
//  QQ Email: 1669499355@qq.com
//  Last Modified time: 2016-09-07 16:16:35
//  Description: LiveClass-ConferApe
//
// //////////////////////////////////////////////////////////////////////////////

import Ape from './Ape';
import ApeConsts from './ApeConsts';
import MessageTypes from 'MessageTypes';
import pdu from 'pdus';
import { Zlib } from 'zlibjs/bin/zlib.min';
import UTF8 from 'utf-8';
import Loger from 'Loger';
let loger = Loger.getLoger('ConferApe');

class ConferApe extends Ape {
  constructor() {
    super(
      ApeConsts.CONFERENCE_SESSION_ID,
      ApeConsts.CONFERENCE_SESSION_NAME,
      ApeConsts.CONFERENCE_SESSION_TAG
    );

    // Attribures
    this.hostNodeId = -1;
    this.hostUserId = '';
    this.rosters = {};
    this.activeDocId = '';

    // Ape Models
    this.registerKey(this._session_id, this._session_name, this._session_tag, new ArrayBuffer);
    this.registerObj(pdu.RCPDU_REG_REGISTER_ROSTER, ApeConsts.CONFERENCE_OBJ_ROSTER_ID,
      ApeConsts.CONFERENCE_OBJ_ROSTER_NAME, ApeConsts.CONFERENCE_OBJ_ROSTER_TAG, 0, new ArrayBuffer);
    this.registerObj(pdu.RCPDU_REG_REGISTER_QUEUE, ApeConsts.CONFERENCE_OBJ_QUEUE_ID,
      ApeConsts.CONFERENCE_OBJ_QUEUE_NAME, ApeConsts.CONFERENCE_OBJ_QUEUE_TAG, 0, new ArrayBuffer);
    this.registerObj(pdu.RCPDU_REG_REGISTER_TABLE, ApeConsts.CONFERENCE_OBJ_TABLE_ID,
      ApeConsts.CONFERENCE_OBJ_TABLE_NAME, ApeConsts.CONFERENCE_OBJ_TABLE_TAG, 0, new ArrayBuffer);
    this.registerObj(pdu.RCPDU_REG_REGISTER_COUNTER, ApeConsts.CONFERENCE_OBJ_COUNTER_ID,
      ApeConsts.CONFERENCE_OBJ_COUNTER_NAME, ApeConsts.CONFERENCE_OBJ_COUNTER_TAG, 0, new ArrayBuffer);

    this.on(pdu.RCPDU_SESSION_JOIN_RESPONSE, this._joinSessionHandler.bind(this));
  }

  _joinSessionHandler(confInfo) {
    let nodeInfoRecordPdu = this.mcu.mcuConfInfo.self;

    let userDataPdu = new pdu['RCNodeInfoUserDataPdu'];
    userDataPdu.qq = '';
    userDataPdu.skype = '';
    userDataPdu.mobile = '';

    nodeInfoRecordPdu.userData = userDataPdu.toArrayBuffer();
    nodeInfoRecordPdu.deviceType = 3;

    let item = new pdu['RCRegistryRosterItemPdu'];
    item.nodeId = nodeInfoRecordPdu.nodeId;
    item.nodeData = nodeInfoRecordPdu.toArrayBuffer();

    let rosterUpdateItem = new pdu['RCRegistryRosterUpdateItemPdu'];
    rosterUpdateItem.type = pdu.RCPDU_REG_ROSTER_UPDATE_PDU;
    rosterUpdateItem.items.push(item);

    let updateObjPdu = new pdu['RCRegistryUpdateObjPdu'];
    updateObjPdu.objId = ApeConsts.CONFERENCE_OBJ_ROSTER_ID;
    updateObjPdu.subType = rosterUpdateItem.type;
    updateObjPdu.userData = rosterUpdateItem.toArrayBuffer();

    let adapterItemPdu = new pdu['RCAdapterItemPdu'];
    adapterItemPdu.type = pdu.RCPDU_REG_UPDATE_OBJ;
    adapterItemPdu.itemData = updateObjPdu.toArrayBuffer();

    let adapterPdu = new pdu['RCAdapterPdu'];
    adapterPdu.type = pdu.RCPDU_REG_ADAPTER;
    adapterPdu.item.push(adapterItemPdu);

    this.sendUniform(adapterPdu, true);


  }


  tableUpdateHandler(owner, recordId, recordData) {
    try {
      let tabUpdatePdu = pdu.RCTabUpdateDataRequestPdu.decode(recordData);
      const uncompressedBytes = new Zlib.Inflate(tabUpdatePdu.action.compact().view).decompress();
      let tabInfo = UTF8.getStringFromBytes(uncompressedBytes);
      let tabTypeMatches = tabInfo.match(/<TabType>(.+)<\/TabType>/);
      if (tabTypeMatches.length > 1 && tabTypeMatches[1] == 'show.docsharing') {
        if (tabInfo.match(/<visible>(.+)<\/visible>/)[1] == 'true') {
          this.activeDocId = tabInfo.match(/<TabID>(.+)<\/TabID>/)[1];
          this._emit(MessageTypes.DOC_SWITCH, this.activeDocId);
        }
      }
    } catch (e) {
      loger.warn('ConferApe table update got exception.');
    }
  }


  rosterInsertHandler(nodeId, nodeData) {
    this.rosterUpdateHandler(nodeId, nodeData);
  }

  //更新人员列表数据
  rosterUpdateHandler(nodeId, nodeData) {

    //loger.log("更新人员列表数据",nodeId,userDataObj);
    if (nodeData.role === ApeConsts.NR_MASTER ||
      nodeData.role === ApeConsts.NR_SLAVE) {
      this.hostNodeId = nodeData.nodeId;
      this.hostUserId = nodeData.userId;
    }
    if (nodeData.role === ApeConsts.NR_NORMAL &&
      this.hostNodeId === nodeData.nodeId) {
      this.hostNodeId = -1;
      this.hostUserId = '';
    }
    let rosterExists = this.rosters[nodeId];
    this.rosters[nodeId] = nodeData;
    if (!rosterExists) {
      let userDataObj=pdu['RCNodeInfoUserDataPdu'].decode(nodeData.userData);
      let newNodeData=nodeData;
      newNodeData.userData=userDataObj;
      loger.log("更新人员列表数据",{"nodeId":nodeId});
      this._emit(MessageTypes.CLASS_INSERT_ROSTER, {"nodeId":nodeId,"nodeData":newNodeData});
      this.emitRosterChange();
    }
  }

  //删除用户
  rosterDelHandler(nodeId) {
    delete this.rosters[nodeId];
    this._emit(MessageTypes.CLASS_DELETE_ROSTER, {"nodeId":nodeId});
    this.emitRosterChange();
    // 自己退出
    if (nodeId == this._confInfo.nodeId) {
      this._emit(MessageTypes.CLASS_EXIT);
    }
  }

  //广播当前的人数
  emitRosterChange() {
    this._emit(MessageTypes.CLASS_SHOW_ROSTER_NUM, Object.keys(this.rosters).length);
  }
}

export default ConferApe;