WhiteBoardApe.js 5.8 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-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';

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

class WhiteBoardApe extends Ape {
  constructor() {
    super(
      ApeConsts.WHITEBOARD_SESSION_ID,
      ApeConsts.WHITEBOARD_SESSION_NAME,
      ApeConsts.WHITEBOARD_SESSION_TAG
    );
    // properties
    this.annoInfos = {};

    //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_CONFERENCE_SEND_DATA_REQUEST, this.whiteboardMsgComingHandler.bind(this));

    // 白板延迟
     this._apeDelayed = true;
  }

  // 添加标注,发送信息
  sendAnnotaion(_param){
    //var objPoint={
    //  "type":0,
    //  "id":111111,
    //  "color":0,
    //  "thickness":0,
    //  "radius":0,
    //  "text":0,
    //  "fontS":0,
    //  "fontN":0,
    //  "pointD":[]
    //};

    //message RCWhiteboardDataRequest2Pdu {
    //  required uint32 id = 1;
    //  required uint32 type = 2;
    //  required uint32 initiator = 3;
    //  required bytes drawData = 4;
    //}
    //message RCWhiteboardCurveLinePdu {
    //  required uint32 color = 1;
    //  required uint32 thickness = 2;
    //  required string pointData = 3;
    //}
    //
    //var coverLine=new pdu['RCWhiteboardCurveLinePdu'];
    //coverLine.color=0xff00ff;
    //coverLine.thickness=2;
    //coverLine.pointData="白板数据测试测试";
    //
    //var sendData=new pdu['RCWhiteboardDataRequest2Pdu'];
    //sendData.id=parseInt(Date.now() / 1000);
    //sendData.type=pdu.RCPDU_AUDIO_SEND_DATA_REQUEST;
    //sendData.initiator = this._confInfo.nodeId;//发起人
    //sendData.peer = parseInt(101);//发送给谁,公聊的时候是0,私聊的时候是指定的用户id
    //sendData.isPublic = true;
    //sendData.drawData=coverLine.toArrayBuffer();
    //console.log("白板发送数据=============================");
    //this.sendUniform(sendData);

    let chatSendPdu = new pdu['RCChatSendDataRequestPdu'];
    chatSendPdu.type = pdu.RCPDU_CONFERENCE_SEND_DATA_REQUEST;
    chatSendPdu.initiator = this._confInfo.nodeId;//发起人
    chatSendPdu.peer = parseInt(0);//发送给谁,公聊的时候是0,私聊的时候是指定的用户id
    chatSendPdu.isPublic = true;
    chatSendPdu.userData = this._rCArrayBufferUtil.strToUint8Array("h5" +"白板测试");
    chatSendPdu.fromName = this._rCArrayBufferUtil.strToUint8Array("h5" + this._confInfo.userName);
    chatSendPdu.fromRole = this._confInfo.classRole;

    //同步
    let adapterItemPdu = new pdu['RCAdapterItemPdu'];
    adapterItemPdu.type = pdu.RCPDU_REG_UPDATE_OBJ;
    adapterItemPdu.itemData = chatSendPdu.toArrayBuffer();

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

    console.log("白板发送数据=============================QQ");
    this.sendUniform(adapterPdu, true);



   // this.sendUniform(chatSendPdu);
    //if (!(chatSendPdu.isPublic || 0 === chatSendPdu.peer)) {
    //  //发送给制定的人
    //  this.send(chatSendPdu);
    //} else {
    //  //发送给所有人
    //  this.sendUniform(chatSendPdu);
    //}
  }
  //删除标注,发送信息
  deleteAnnotaion(_param){
  }

  whiteboardMsgComingHandler(pdu) {
      //loger.warn('whiteboardMsgComingHandler needs to be handled.');
    console.log("白板收到数据=============================QQ");
  }

  wbReceriveHandler(chatBuffer) {
    console.log("白板收到数据=============================QQ");
    //var chatReceivePdu = pdu['RCChatSendDataRequestPdu'].decode(chatBuffer);
    //
    //var chatMsg = {};
    //chatMsg.fromNodeID = chatReceivePdu.initiator;
    //chatMsg.toNodeID = chatReceivePdu.peer;
    //chatMsg.message = this._rCArrayBufferUtil.uint8ArrayToStr(chatReceivePdu.userData, 2);
    //chatMsg.fromName = this._rCArrayBufferUtil.uint8ArrayToStr(chatReceivePdu.fromName, 2);
    //chatMsg.fromRole = chatReceivePdu.fromRole;
    //
    //loger.log('接收聊天消息.', chatMsg);
    //
    //this._emit(MessageTypes.CHAT_RECEIVE, chatMsg);
  }
  tableInsertHandler(tableId, record) {
      this.emitDocChange(ApeConsts.DOCUMENT_DEL);
  }

  tableUpdateHandler(owner, recordId, recordData) {
    const recordInfo = pdu['RCWhiteboardDataRequestPdu'].decode(recordData);
    console.log("recordInfo");
    console.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)
      };
      this.annoInfos[recordId] = annoInfo;
      this._emit(MessageTypes.ANNOTATION_UPDATE, annoInfo);
    } else {
      loger.log('白板动作忽略,类型:', ApeConsts(recordInfo.type));
    }
  }
}

export default WhiteBoardApe;