import { Dictionary, Common, userInfo, GameInfo } from "../Common"; // let targeOrigin = "http://127.0.0.1:8080"; let targeOrigin = "*"; // 最后处理过的消息 let lastInfo = { timeStamp: 0 } let events: Dictionary<IEvent[]> = { } export enum EUserGameState { /** 加载中 */ loading = 0, /** 已加载 */ loaded, /** 准备完毕 */ ready, /** 作答中 */ playing, /** 暂停中 */ paused, /** 已完成 */ complete } export let UserGameStateMap: Dictionary<any> = { 0: '正在加载...', 1: '已加载', 2: '准备完毕', 3: '作答中', 4: '暂停中', 5: '已完成' } /** action */ export enum EZMWebAction { NONE = 0, /** 准备完毕, 可以接受用户信息 */ gameReady, /** 设置用户信息 */ setUserInfo, /** 设置课堂所有用户信息 */ setUsersInfo, /** 客户端控制显示隐藏 */ setVisible, /** 开始游戏 */ STARTGAME, /** 暂停游戏 */ PAUSEGAME, /** 恢复游戏 */ RESUMEGAME, /** 结束游戏 */ ENDGAME, /** 同步状态 */ SYSTATUS, } interface IEvent { caller: any, func: Function } export interface IActionData { action: string, data: any, source?: string; /** 固定为gameId */ id?: number; timeStamp?: number; } let isFrameWinows = window.parent && window.parent.window; export function postMessage(actionData: IActionData) { console.log('发送信息', actionData); // if (!actionData.data !actionData.data.userId) { // actionData.from = userInfo.userId || null; // actionData // } if (!actionData.data.id && userInfo.userId) { actionData.data.id = userInfo.userId || null; actionData.data.name = userInfo.userName || null; } if (!actionData.id) { actionData.id = GameInfo.gameId; actionData.source = 'game'; actionData.timeStamp = +new Date(); } isFrameWinows && isFrameWinows.postMessage(actionData, targeOrigin); } export function onAction(action: string, cb: Function, caller: any) { let actionEvent = events[action] || []; actionEvent.push({ caller, func: cb }); events[action] = actionEvent; console.log('监听事件', action, cb, events) } export function offAction(action: string, cb: Function, caller: any) { let actionEvent = events[action] || []; let index = actionEvent.indexOf({ caller, func: cb }); actionEvent.splice(index, 1); } function receiveMessage(event) { console.log({ '我们能信任信息来源吗?': event }) // 我们能信任信息来源吗? // if (event.origin !== "http://example.com:8080") // return; let data: IActionData = event.data; let action = data.action; console.log(' events[action]:', events, action, data.timeStamp, lastInfo.timeStamp) if (action && events[action]) { if (data.timeStamp && data.timeStamp < lastInfo.timeStamp) { console.log('过期消息不执行') return; } if (data.timeStamp) { lastInfo.timeStamp = data.timeStamp; } // 执行回调 events[action].forEach(item => { console.log('执行回调', item); item.func.call(item.caller, event); }); } // event.source 就当前弹出页的来源页面 // event.data 是 "hello there!" // 假设你已经验证了所受到信息的origin (任何时候你都应该这样做), 一个很方便的方式就是把enent.source // 作为回信的对象,并且把event.origin作为targetOrigin } /** 添加事件监听 */ window.addEventListener("message", receiveMessage, false); onAction(EZMWebAction[EZMWebAction.setUserInfo], (event) => { console.log('setUserInfo EVENT:', event.data); let eventData = event.data; let data = eventData.data; userInfo.userId = data.id; userInfo.userName = data.name; }, this); export function registerSetUserInfo() { onAction(EZMWebAction[EZMWebAction.setUsersInfo], (event) => { console.log('setUsersInfo EVENT:', event) }, this); } cc.game.on(cc.game.EVENT_HIDE, function () { console.log('进入后台'); cc.game.pause(); return; }); cc.game.on(cc.game.EVENT_SHOW, function () { console.log('进入前台'); cc.game.resume(); return; }); onAction(EZMWebAction[EZMWebAction.setVisible], (event) => { let data = event.data.data; let visible = data.value; console.log('---setVisible-', data); if (visible) { cc.game.resume(); } else { cc.game.pause(); } }, this);