ChatApe.js 3.5 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-14 15:55:42
//  QQ Email: 1669499355@qq.com
//  Last Modified time: 2016-09-06 16:28:14
//  Description: LiveClass-ChatApe
//
// //////////////////////////////////////////////////////////////////////////////

import Ape from './Ape';
import ApeConsts from './ApeConsts';
import pdu from 'pdus';
import Loger from 'Loger';
import MessageTypes from 'MessageTypes';
import EngineUtils from "EngineUtils";
let loger = Loger.getLoger('ChatApe');

class ChatApe extends Ape {
  constructor() {
    super(
      ApeConsts.CHAT_SESSION_ID,
      ApeConsts.CHAT_SESSION_NAME,
      ApeConsts.CHAT_SESSION_TAG
    );

    //Ape Models
    this.registerKey(this._session_id, this._session_name, this._session_tag, new ArrayBuffer);
    this.registerObj(pdu.RCPDU_REG_REGISTER_TABLE, ApeConsts.CHAT_OBJ_TABLE_ID,
      ApeConsts.CHAT_OBJ_TABLE_NAME, ApeConsts.CHAT_OBJ_TABLE_TAG, 0, new ArrayBuffer);

    // ape listeners
    //this.on(pdu.RCPDU_CHAT_SEND_DATA_REQUEST, this.chatMsgIncomingHandler.bind(this));
    this.on(pdu.RCPDU_SEND_CHAT_DATA_REQUEST, this.chatMsgIncomingHandler.bind(this));
  }

  sendChatMsg(_messageInfo) {
    if(this._classInfo===null||EngineUtils.isEmptyObject(this._classInfo)){
      loger.log('不能发送聊天消息.McuClient还未初始化数据!');
      if(GlobalConfig.getCurrentStatus().code==0||GlobalConfig.getCurrentStatus().code==1){
        this._emit(MessageTypes.MCU_ERROR,MessageTypes.ERR_APE_SEND_FAILED_NO_JOIN);
        return;
      }
      return ;
    }

   // to, message
    loger.log('发送聊天消息.', _messageInfo.to, _messageInfo.message);

    let chatSendPdu = new pdu['RCChatSendDataRequestPdu'];
    //chatSendPdu.type = pdu.RCPDU_CHAT_SEND_DATA_REQUEST;
    chatSendPdu.type = pdu.RCPDU_SEND_CHAT_DATA_REQUEST;
    chatSendPdu.initiator = this._classInfo.nodeId;//发起人
    chatSendPdu.peer = parseInt(_messageInfo.to);//发送给谁,公聊的时候是0,私聊的时候是指定的用户id

    chatSendPdu.userData = this._rCArrayBufferUtil.strToUint8Array("h5" + _messageInfo.message);
    chatSendPdu.fromName = this._rCArrayBufferUtil.strToUint8Array("h5" + this._classInfo.userName);
    chatSendPdu.fromRole = this._classInfo.userRole;//    classRole已经废弃
    chatSendPdu.isPublic = true;
    // if (!(chatSendPdu.isPublic || 0 === chatSendPdu.peer)) {
    if (!chatSendPdu.isPublic && 0!=chatSendPdu.peer) {
      //发送给制定的人
      //loger.log('发送私聊消息.');
      this.send(chatSendPdu);
    } else {
      //发送给所有人
      //loger.log('发送公聊消息.');
      //this.sendUniform(chatSendPdu);
      this.sendChatUniform(chatSendPdu);
    }
  }

  chatMsgIncomingHandler(chatBuffer) {
    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);
  }
}

export default ChatApe;