import SDKPacket from "./SDKPacket"; import SDKPacketPool from "./SDKPacketPool"; import SDKCommandBase from "../SDKCommand/SDKCommandBase"; import * as ZMSDK from "../ZMSDK"; import SDKUserInfo from "../SDKLogics/SDKUserInfo"; import * as SDKLogicsCore from "../SDKLogics/SDKLogicsCore"; import * as SDKENUM from "../SDKConst/SDKEnum"; import * as SDKRegistCommand from "../SDKConst/SDKRegistCommand"; /** * SDKPacketHandler: * 消息包处理类,对上接SDKTransceiver,对下接游戏处理逻辑 * 改类目前是处理postMessage * 如果要处理TCP等 可以继承该类, 重写相应的方法即可 */ export default class SDKPacketHandler { protected _packets: Array; protected _messageHanlers: { [key: string]: any }; constructor() { this._packets = []; this._messageHanlers = {}; } /** * 处理消息 */ private messageHandle(): void { let packet: SDKPacket; if (this._packets.length == 0) { return; } packet = this._packets.shift(); console.log("处理数据包....", packet); console.log("url参数信息。。。", SDKLogicsCore.parameterVo); //subIframe接收到的消息,,,,也就是分屏接收到的消息 if (ZMSDK.isSplitScreen()) { console.log("分屏子iframe处理数据包....", packet); this.dispatcherMessage(packet); return; } if (SDKLogicsCore.parameterVo.userId == '') { console.error("setUserInfo没初始化。。。或者mobileId为0"); } //自己收到自己发送过来的消息,不用做任何处理 if (packet.sendId == SDKLogicsCore.parameterVo.userId && SDKLogicsCore.parameterVo.userId != '') { console.log("自己收到自己发送过来的消息,不用做任何处理"); return; } //系统系统消息必须处理 if (SDKRegistCommand.EVENTS_LIST.indexOf(packet.name) != -1 || SDKRegistCommand.EVENT_CHECK.indexOf(packet.name) != -1) { console.log("系统消息......", packet.name); if (this.isNeedSave(packet)) { this.saveUserMessage(packet); } this.dispatcherMessage(packet); return; } //保存消息 this.saveUserMessage(packet); if (ZMSDK.isStudent()) { console.log("学生处理。。。。。"); //学生端 if (!SDKLogicsCore.controllState.isOwn()) { //控制权不在自己手里。。。执行发送过来的消息 this.dispatcherMessage(packet); } else { //控制权在自己手里。。。不做任何操作 console.log("学生控制权在自己手里。。。不做任何操作"); } } if (ZMSDK.isTeacher()) { console.log("老师处理。。。。。"); //老师端 if (!SDKLogicsCore.controllState.isOwn()) { //控制权不在自己手里。。。 console.log("控制权不在老师手里...."); if (SDKLogicsCore.controllState.teachingMode == SDKENUM.TEACHING_MODE.TYPE_TEACHING) { //教学模式。。。 if (SDKLogicsCore.controllState.controllerId != '') { //某个学生上台。。。同步这个上台学生 if (packet.sendId == SDKLogicsCore.controllState.controllerId) { this.dispatcherMessage(packet); console.log("控制权不在老师手里.....教学模式,某一个学生上台"); } } else { console.log("控制权不在老师手里,教学模式,不做任何处理"); } } else { //分屏查看模式。。。向子iframe发送消息包 console.log("控制权不在老师手里.....分屏模式"); this.dispatcherSubIframe(packet); } } else { //控制权在自己手里。。。 console.log("控制权在老师手里....."); if (SDKLogicsCore.controllState.teachingMode == SDKENUM.TEACHING_MODE.TYPE_TEACHING) { //教学模式。。。不做任何操作 console.log("控制权在老师手里,教学模式,不做任何处理"); } else if (SDKLogicsCore.controllState.teachingMode == SDKENUM.TEACHING_MODE.TYPE_INSPECTION) { //分屏查看模式。。。向子iframe发送消息包 console.log("控制权在老师手里.....分屏模式"); this.dispatcherSubIframe(packet); } } } if (ZMSDK.isObserverClass()) { console.log("监课模式......"); if (this.packetIsObserved(packet)) { if (SDKLogicsCore.parameterVo.isOberverTeacher()) { if (SDKLogicsCore.controllState.teachingMode == SDKENUM.TEACHING_MODE.TYPE_TEACHING) { if(packet.sendId == SDKLogicsCore.parameterVo.observerId){ this.dispatcherMessage(packet); } } else { this.dispatcherSubIframe(packet); } } else { this.dispatcherMessage(packet); } } else { console.error("监课数据包异常......"); } } } /** * 是否是被监视的包 */ private packetIsObserved(packet: SDKPacket): boolean { let result: boolean = true; let userInfo: SDKUserInfo; if (packet.sendId == "") { console.log("消息发送者为空......"); result = false; return; } if (SDKLogicsCore.parameterVo.observerId == "") { result = false; console.log("被监视的对象为空......"); return; } userInfo = SDKLogicsCore.userInfos.getUserInfoById(SDKLogicsCore.parameterVo.observerId); if (!userInfo) { result = false; console.log("被监视的对象为不存在......", SDKLogicsCore.parameterVo.observerId); console.log("伙伴列表......", SDKLogicsCore.userInfos); return; } if (userInfo.role == SDKENUM.USER_ROLE.STUDENT) { if (SDKLogicsCore.controllState.controllerId == "-1") { if (packet.sendId != userInfo.userId) { result = false; console.log("被监视的对象与包的发送者不同1......", packet.sendId, userInfo.userId); return; } } else if (SDKLogicsCore.controllState.controllerId != "") { if (packet.sendId != SDKLogicsCore.controllState.controllerId) { result = false; console.log("被监视的对象与包的发送者不同2......", packet.sendId, userInfo.userId); return; } } } else if (userInfo.role == SDKENUM.USER_ROLE.TEACHER) { userInfo = SDKLogicsCore.userInfos.getUserInfoById(packet.sendId); if (!userInfo) { result = false; console.log("被监视的对象与包的发送者不同(老师)......"); return; } } return result; } /** * 保存消息包.....每一个端都拥有所有玩家的所有数据 * @param packet 消息包 */ public saveUserMessage(packet: SDKPacket): void { let userInfo: SDKUserInfo; let clonePacket: SDKPacket; let index = 0, count = 0; count = SDKLogicsCore.userInfos.getCount(); for (index = 0; index < count; index++) { userInfo = SDKLogicsCore.userInfos.getUserInfoByIndex(index); if (!userInfo) { continue; } if (SDKRegistCommand.EVENTS_LIST.indexOf(packet.name) == -1 && SDKLogicsCore.controllState.controllerId == '-1') { //不是系统消息。。。控制权限是各玩个的。。。 if (packet.sendId != userInfo.userId) { //消息发送者 不是 对应user. 不保存 continue; } } if (!this.isNeedSave(packet)) { continue; } clonePacket = SDKPacketPool.Acquire(packet.name); packet.clone(clonePacket); userInfo.addPacket(clonePacket); console.log("保存数据包。。。保存在", userInfo.userId, clonePacket); } } private isNeedSave(packet: SDKPacket): boolean { if (SDKRegistCommand.EVENTS_NO_SAVE.indexOf(packet.name) != -1 || SDKRegistCommand.EVENT_CHECK.indexOf(packet.name) != -1) { return false; } return true; } /** * 执行接收到的消息 * @param packet */ private dispatcherMessage(packet: SDKPacket): void { let command: SDKCommandBase; command = this.getHandler(packet.name); if (!command) { console.log("没有注册命令:........[", packet.name + "]"); console.log("已注册的命令:........", this._messageHanlers); return; } command.packet = packet; command.execute(packet.data); } /** * 向老师端的子iframe派发消息 * @param packet */ private dispatcherSubIframe(packet: SDKPacket): void { let command: SDKCommandBase; command = this.getHandler(SDKRegistCommand.DISPATCHER_TO_SUBIFRAME); if (!command) { console.log(SDKRegistCommand.DISPATCHER_TO_SUBIFRAME + ":没注册命令"); return; } command.packet = packet; command.execute(packet.data); } /** * 接收到消息 * @param packet 消息包 */ public recevieMsg(packet: SDKPacket): void { this._packets.push(packet); while (this._packets.length > 0) { this.messageHandle(); } } /** *控制者发送消息,立马执行派发,提高游戏体验 * @param packet 消息包 */ public sendToReceive(packet: SDKPacket): void { this.saveUserMessage(packet); this.dispatcherMessage(packet); } /** * 外界调用。。。。发送CMD * @param packet */ public dispatcherCMD(packet: SDKPacket): void { this.dispatcherMessage(packet); } public registerHandler(name: string, value: any): void { this._messageHanlers[name] = value; } public unRegisterHandler(name: string): void { this._messageHanlers[name] = null; delete this._messageHanlers[name]; } /** * 获得CMD对象 * @param name CMD的名称 */ public getHandler(name: string): SDKCommandBase { if (!this._messageHanlers[name]) { console.log("消息对应的命令不存在.....[", name + "]"); return null; } return new this._messageHanlers[name]() as SDKCommandBase; } public reset(): void { let index = 0, count = 0; count = this._packets.length; for (index = 0; index < count; index++) { SDKPacketPool.Release(this._packets[index]); } this._packets = []; } public destroy(): void { this.reset(); this._packets = null; this._messageHanlers = null; } }