WhiteBoardApe.js 16.7 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
// //////////////////////////////////////////////////////////////////////////////
//
//  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-26 17:36:20
//  QQ Email: 1669499355@qq.com
//  Last Modified time: 2016-09-21 14:06:12
//  Description: LiveClass-WhiteBoardApe
//
// //////////////////////////////////////////////////////////////////////////////

import Ape from './Ape';
import ApeConsts from './ApeConsts';
import pdu from 'pdus';
import Loger from 'Loger';
import MessageTypes from 'MessageTypes';
import { Zlib } from 'zlibjs/bin/zlib.min';
import UTF8 from 'utf-8';
import GlobalConfig from 'GlobalConfig';
import EngineUtils from 'EngineUtils';

let loger = Loger.getLoger('WhiteBoardApe');

let itemIdx=0;//table插入新数据的计数id,目前用时间戳
const TYPE_BIGHT=0;//任意线段
const TYPE_LINE=1;//直线
const TYPE_RECT=2;//矩形
const TYPE_CIRCLE=3;//圆形
const TYPE_TEXT=4;//文本

class WhiteBoardApe extends Ape {
  constructor() {
    super(
      ApeConsts.WHITEBOARD_SESSION_ID,
      ApeConsts.WHITEBOARD_SESSION_NAME,
      ApeConsts.WHITEBOARD_SESSION_TAG
    );
    // properties
    this.annoInfos = {};
    // 白板延迟
    // this._apeDelayed = true;

    //Ape Models
    this.registerKey(this._session_id, this._session_name, this._session_tag, new ArrayBuffer);
    this.registerObj(pdu.RCPDU_REG_REGISTER_TABLE, ApeConsts.WHITEBOARD_OBJ_TABLE_ID,
      ApeConsts.WHITEBOARD_OBJ_TABLE_NAME, ApeConsts.WHITEBOARD_OBJ_TABLE_TAG, 0, new ArrayBuffer);

    // ape listeners
    this.on(pdu.RCPDU_SESSION_JOIN_RESPONSE, this._joinSessionHandler.bind(this));
    //this.on(pdu.RCPDU_CONFERENCE_SEND_DATA_REQUEST, this.whiteboardMsgComingHandler.bind(this));//这个是会议消息类型,flash里在使用这里不再使用,各个模块的消息由模块自己来处理
  }

  _joinSessionHandler(_data) {
    loger.log("RCPDU_SESSION_JOIN_RESPONSE");
  }

  /////////////发送数据操作//////////////////////////////////////////////////////
  // 添加标注,发送信息
  sendInsetAnnotaion(_param){
/*    itemIdx=parseInt(Date.now()/1000);
    let whiteBoardModelPdu =new pdu['RCWhiteBoardDataModel'];
    whiteBoardModelPdu.id =itemIdx;
    whiteBoardModelPdu.initiator =GlobalConfig.nodeId;
    whiteBoardModelPdu.pointGroup =EngineUtils.arrayToJsonString([{"w":10,"h":20},{"w":10,"h":20},{"w":10,"h":20},{"w":10,"h":20}]);
    whiteBoardModelPdu.type = 3;

  */

    /* let whiteBoardModelPdu =new pdu['RCWhiteBoardDataModel'];
     whiteBoardModelPdu.id =ApeConsts.WHITEBOARD_OBJ_TABLE_ID;
     whiteBoardModelPdu.initiator =GlobalConfig.nodeId;
     whiteBoardModelPdu.pointGroup =EngineUtils.arrayToJsonString([{"w":133,"h":20},{"w":1313,"h":20},{"w":313,"h":20},{"w":10,"h":20}]);
     whiteBoardModelPdu.type = 3;
     */

    if(_param==null||EngineUtils.isEmptyObject(_param)){
      loger.log('sendInsetAnnotaion失败,参数错误');
      this._emit(MessageTypes.MCU_ERROR,MessageTypes.ERR_APE_INTERFACE_PARAM_WRONG);
      return ;
    }

      itemIdx=EngineUtils.creatTimestamp();
    let whiteBoardModelPdu = this.packPdu(_param,itemIdx);
      if(whiteBoardModelPdu==null){
          loger.log('sendInsetAnnotaion失败,参数错误');
          this._emit(MessageTypes.MCU_ERROR,MessageTypes.ERR_APE_INTERFACE_PARAM_WRONG);
          return ;
      }
      console.log(whiteBoardModelPdu);

      let tableItemPdu = new pdu['RCRegistryTableItemPdu'];
    tableItemPdu.itemIdx=itemIdx;//直接用时间戳作为id
    tableItemPdu.registerObjId=ApeConsts.WHITEBOARD_OBJ_TABLE_ID;
    tableItemPdu.owner = 0;//收到flash的是这个值,不清楚先写固定
    tableItemPdu.itemData =whiteBoardModelPdu.toArrayBuffer();

  //insert
    let tableInsertItemPdu = new pdu['RCRegistryTableInsertItemPdu'];
    //optional RCPduType_E type = 1 [default = RCPDU_REG_TABLE_UPDATE_PDU];
    //repeated RCRegistryTableItemPdu items = 2;
    tableInsertItemPdu.type = pdu.RCPDU_REG_TABLE_INSERT_PDU;//
    tableInsertItemPdu.items.push(tableItemPdu);

    let updateObjPdu = new pdu['RCRegistryUpdateObjPdu'];
    updateObjPdu.objId = ApeConsts.WHITEBOARD_OBJ_TABLE_ID;
    updateObjPdu.subType = tableInsertItemPdu.type;
    updateObjPdu.userData = tableInsertItemPdu.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);

    console.log("添加白板数据=====itemIdx="+tableItemPdu.itemIdx);
    this.sendUniform(adapterPdu,true);
  }
  //删除所有标注
  sendDeleteAllAnnotation(_param){
    for (let key in this.annoInfos){
      //console.log("key:"+key);
      console.log("删除白板数据,itemIdx:"+key);
      this.sendDeleteAnnotaion({"itemIdx":key});
    }
  }
  //删除标注,发送信息
  sendDeleteAnnotaion(_param){
    //{"itemIdx":itemIdx}
    let tableDeleteItemPdu = new pdu['RCRegistryTableDeleteItemPdu'];
    //optional RCPduType_E type = 1 [default = RCPDU_REG_TABLE_DELETE_PDU];
   // repeated uint32 item_idx = 2;
    tableDeleteItemPdu.type = pdu.RCPDU_REG_TABLE_DELETE_PDU;//
    tableDeleteItemPdu.itemIdx=parseInt(_param.itemIdx);//这里需要设置要删除的数据的itemIdx,每条数据的这个id都不一样

    let updateObjPdu = new pdu['RCRegistryUpdateObjPdu'];
    updateObjPdu.objId = ApeConsts.WHITEBOARD_OBJ_TABLE_ID;
    updateObjPdu.subType = tableDeleteItemPdu.type;
    updateObjPdu.userData = tableDeleteItemPdu.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);

    console.log("白板发送删除数据============="+tableDeleteItemPdu.itemIdx);
    this.sendUniform(adapterPdu,true);
  }


  //更新标注
  sendUpdaterAnnotaion(_param){
      if(_param==null||EngineUtils.isEmptyObject(_param)){
          loger.log('sendUpdaterAnnotaion失败,参数错误');
          this._emit(MessageTypes.MCU_ERROR,MessageTypes.ERR_APE_INTERFACE_PARAM_WRONG);
          return ;
      }

       /* let whiteBoardModelPdu =new pdu['RCWhiteBoardDataModel'];
        whiteBoardModelPdu.id =ApeConsts.WHITEBOARD_OBJ_TABLE_ID;
        whiteBoardModelPdu.initiator =GlobalConfig.nodeId;
        whiteBoardModelPdu.pointGroup =EngineUtils.arrayToJsonString([{"w":133,"h":20},{"w":1313,"h":20},{"w":313,"h":20},{"w":10,"h":20}]);
        whiteBoardModelPdu.type = 3;
      */

      itemIdx=_param.itemIdx;
      let whiteBoardModelPdu = this.packPdu(_param,itemIdx);
      //console.log(whiteBoardModelPdu);

      if(whiteBoardModelPdu==null){
          loger.log('sendInsetAnnotaion失败,参数错误');
          this._emit(MessageTypes.MCU_ERROR,MessageTypes.ERR_APE_INTERFACE_PARAM_WRONG);
          return ;
      }

      let tableItemPdu = new pdu['RCRegistryTableItemPdu'];
      tableItemPdu.itemIdx=itemIdx;
      tableItemPdu.owner = 0;//收到flash的是这个值,不清楚先写固定
      tableItemPdu.registerObjId=ApeConsts.WHITEBOARD_OBJ_TABLE_ID;
      tableItemPdu.itemData =whiteBoardModelPdu.toArrayBuffer();


      //updater
       let whiteBoardUpdateItem = new pdu['RCRegistryTableUpdateItemPdu'];
       //optional RCPduType_E type = 1 [default = RCPDU_REG_TABLE_UPDATE_PDU];
       //repeated RCRegistryTableItemPdu items = 2;
       whiteBoardUpdateItem.type = pdu.RCPDU_REG_TABLE_UPDATE_PDU;//
       whiteBoardUpdateItem.items.push(tableItemPdu);


      let updateObjPdu = new pdu['RCRegistryUpdateObjPdu'];
      updateObjPdu.objId = ApeConsts.WHITEBOARD_OBJ_TABLE_ID;
      updateObjPdu.subType = whiteBoardUpdateItem.type;
      updateObjPdu.userData = whiteBoardUpdateItem.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);

      console.log("白板发送更新数据===============22");
      this.sendUniform(adapterPdu,true);
  }

  ///////白板数据的封包和解包/////////////////////////////////////////
  packPdu(_param,_itemIdx){
      /* required uint32 type= 1;//白板类型
       required uint32 id= 2;//id 每一次绘制的唯一标识
       required uint32 initiator=3; //绘制来自谁
       optional string pointGroup=4; //坐标点集数组的JSON字符串
       optional string color= 5  [default = "#000000"]; //颜色
       optional uint32 thickness= 6 ;//线条粗细
       optional uint32 radius= 7;//园的半径
       optional uint32 fontSize= 8;//字体大小
       optional uint32 fontName= 9;//字体名称
       optional uint32 text= 10;//文本内容*/

      //验证坐标点集合数组是否合法
      if(_param.pointGroup==null||_param.pointGroup.length<1){
          this._emit(MessageTypes.MCU_ERROR,MessageTypes.ERR_APE_INTERFACE_PARAM_WRONG);
          return null;
      }
      //判断type类型,根据type设置不同的参数
      let whiteBoardModelPdu =new pdu['RCWhiteBoardDataModelPdu'];
      switch (_param.type){
          case TYPE_BIGHT:
                    break;
              case TYPE_LINE:
                    break;
              case TYPE_RECT:
                break;
              case TYPE_CIRCLE:
                  whiteBoardModelPdu.radius=parseInt(_param.radius);
                break;
              case TYPE_TEXT:
                  whiteBoardModelPdu.fontSize=parseInt(_param.fontSize);
                  whiteBoardModelPdu.fontName=_param.fontName||null;
                  whiteBoardModelPdu.text=_param.text||null;
                break;
              default:
                  //其它类型不做处理
                  return null;
                    break;
      }
      //下面4个是必须的参数
      whiteBoardModelPdu.type =_param.type;
      whiteBoardModelPdu.itemIdx =_itemIdx;
      whiteBoardModelPdu.initiator =GlobalConfig.nodeId;

     /* whiteBoardModelPdu.parentId=_param.parentId||0;
      whiteBoardModelPdu.curPage=_param.curPage||1;*/

      whiteBoardModelPdu.parentId=GlobalConfig.activeDocId;//当前激活的文档id
      whiteBoardModelPdu.curPageNo=GlobalConfig.activeDocCurPage;//当前激活的文档页码

      whiteBoardModelPdu.pointGroup =EngineUtils.arrayToJsonString(_param.pointGroup);
      whiteBoardModelPdu.color=_param.color||"#000000";

      return whiteBoardModelPdu;
  }
  unPackPdu(owner, itemIdx,itemData){
      try{
          console.log("白板收到数据===unPackPdu ");
          let whiteBoardModelPdu= pdu['RCWhiteBoardDataModelPdu'].decode(itemData);
          //console.log(whiteBoardModelPdu);
          //loger.log(whiteBoardModelPdu);
          let _pointGroup= EngineUtils.arrayFromJsonString(whiteBoardModelPdu.pointGroup);
          whiteBoardModelPdu.pointGroup=_pointGroup;
          this.annoInfos[itemIdx] = whiteBoardModelPdu;
          return whiteBoardModelPdu;
      }catch (err){
          console.log("unPackPdu Pdu解析错误,itemIdx="+itemIdx+"  err:"+err.message);
      }
      return null;
  }

  /////收到消息处理/////////////////////////////////////////////////////////////////////////////////
/*  whiteboardMsgComingHandler(_data) {
      //flash    RCConferenceSendDataRequestPdu
      //loger.warn('whiteboardMsgComingHandler needs to be handled.');
      //const recordInfo = pdu['RCWhiteboardDataRequestPdu'].decode(pdu);
      //loger.log("whiteboardMsgComingHandler",recordInfo);

      let receiveInfo = pdu['RCConferenceSendDataRequestPdu'].decode(_data);
      loger.log("whiteboardMsgComingHandler",receiveInfo);
  }*/

  tableInsertHandler(owner, itemIdx,itemData) {
  /*  try{
      console.log("白板收到数据===tableInsertHandler ");
      let whiteBoardModelPdu= pdu['RCWhiteBoardDataModel'].decode(itemData);
      this.annoInfos[itemIdx] = itemData;
      console.log(whiteBoardModelPdu);
    }catch (err){
      console.log("tableInsertHandler Pdu解析错误,itemIdx="+itemIdx+"  err:"+err.message);
    }*/
      let whiteBoardModel=this.unPackPdu(owner, itemIdx,itemData);
      loger.log('tableInsertHandler',whiteBoardModel);
      if(whiteBoardModel){
          if(GlobalConfig.activeDocId==whiteBoardModel.parentId&&GlobalConfig.activeDocCurPage==whiteBoardModel.curPageNo){
              loger.log('WHITEBOARD_ANNOTAION_INSERT 显示到界面上',whiteBoardModel);
              this._emit(MessageTypes.WHITEBOARD_ANNOTAION_INSERT,whiteBoardModel);
          }
      }
  }

  tableUpdateHandler(owner, itemIdx, itemData) {
      let whiteBoardModel=this.unPackPdu(owner, itemIdx,itemData);
      loger.log('tableUpdateHandler',whiteBoardModel);
      if(whiteBoardModel){
          if(GlobalConfig.activeDocId==whiteBoardModel.parentId&&GlobalConfig.activeDocCurPage==whiteBoardModel.curPageNo){
              loger.log('WHITEBOARD_ANNOTATION_UPDATE 显示到界面上',whiteBoardModel);
              this._emit(MessageTypes.WHITEBOARD_ANNOTATION_UPDATE,whiteBoardModel);
          }
      }

  /*  try{
      console.log("白板收到数据===tableUpdateHandler ");
      let whiteBoardModelPdu= pdu['RCWhiteBoardDataModel'].decode(itemData);
      this.annoInfos[itemIdx] = itemData;
      console.log(whiteBoardModelPdu);
      loger.log(whiteBoardModelPdu)
    }catch (err){
      console.log("tableUpdateHandler Pdu解析错误,itemIdx="+itemIdx+"  err:"+err.message);
    }*/

    /*const recordInfo = pdu['RCWhiteboardDataRequestPdu'].decode(recordData);
    console.log("recordInfo");
    loger.log(recordInfo);

    // 目前只处理 文档标注
    if (recordInfo.type == ApeConsts.WBA_DOC_ANNOTATION) {
      const uncompressedBytes = new Zlib.Inflate(recordInfo.action.compact().view).decompress();
      const annoInfo = {
        id: recordId,
        svg: UTF8.getStringFromBytes(uncompressedBytes)
      };
      loger.log(annoInfo);
      this.annoInfos[recordId] = annoInfo;
      this._emit(MessageTypes.WHITEBOARD_ANNOTATION_UPDATE, annoInfo);
    } else {
      loger.log('白板动作忽略,类型:', ApeConsts(recordInfo.type));
    }*/

  }
  tableDeleteHandler(object_id, tableDeleteData){
   // console.log("白板收到数据,tableDeleteHandler  object_id="+object_id);//((18<< 16) + 1)=1179649
      loger.log('tableDeleteHandler',object_id,tableDeleteData);//["tableDeleteHandler",1179649,{"type":231,"itemIdx":[1486301768]}]
      if(tableDeleteData&&tableDeleteData.itemIdx){
          let len=tableDeleteData.itemIdx.length;
          let itemIdxs=tableDeleteData.itemIdx;
          for (let i=0;i<len;i++){
              if(this.annoInfos[itemIdxs[i]]){
                  loger.log("删除白板数据:",itemIdxs[i],"当前剩余的白板数据");
                  console.log(this.annoInfos); this._emit(MessageTypes.WHITEBOARD_ANNOTAION_DELETE,{ "itemIdx":itemIdxs[i]});
                  this._emit(MessageTypes.WHITEBOARD_ANNOTAION_DELETE,{ "itemIdx":itemIdxs[i]});
                  delete this.annoInfos[itemIdxs[i]];
              }
          }
      }
  }
  //文档更新,白板也要更新
  docUpdateHandler(_data){
      loger.log("白板收到文档更新的消息docUpdateHandler",_data);
      for (let key in this.annoInfos){
          let item=this.annoInfos[key];
          if(item&&item.parentId==GlobalConfig.activeDocId&&item.curPageNo==GlobalConfig.activeDocCurPage){
              loger.log("显示和文档对应的白板数据docUpdateHandler",item);
          }else{
              loger.log("不显示白板数据docUpdateHandler",item);
          }
      }

    /*  let docItem=_data;
      for (let key in this.annoInfos){
          let item=this.annoInfos[key];
          if(item&&docItem.visible==true&&item.parentId==docItem.itemIdx&&item.curPageNo==docItem.curPageNo){
              loger.log("显示和文档对应的白板数据docUpdateHandler",item);
          }else{
              loger.log("不显示白板数据docUpdateHandler",item);
          }
      }*/
  }
  //删除白板数据
  docDeleteHandler(_parentId){
      loger.log("白板收到文档删除的消息docDeleteHandler",_parentId);
      for (let key in this.annoInfos){
          let item=this.annoInfos[key];
          if(item&&item.parentId==_parentId){
              loger.log("文档删除,白板数据也删除,itemIdx:"+key,"_parentId:",_parentId);
              this.sendDeleteAnnotaion({"itemIdx":key});
          }
      }
  }
}

export default WhiteBoardApe;