import { TipsLayer } from "../../prefab/layer/TipsLayer"; import { Tips } from "../mgr/Tips"; import { WS, WsRequest, WsResponse } from "../net/WS"; export default class WsM { private static _ins: WsM; public static get ins(): WsM { return (WsM._ins ??= new WsM()); } private _lastShowTimeoutDialogTime: number = 0; public async syncRequest( protocolId: number, data: any, timeout: number = 5000 ): Promise { return new Promise((resolve, reject) => { let getData = false; let cb = (msg: WsResponse) => { getData = true; console.log("websocket msg", msg); WS.ins.off(protocolId, cb, cb); if (msg.success == true) { if (msg.data) { resolve(msg.data); } else { resolve({}); } } else { console.error(msg); Tips.show(msg.msg); resolve(null); } }; WS.ins.on(protocolId, cb, cb); WsRequest.create(protocolId).setData(data).send(); setTimeout(() => { // WS.ins.off(protocolId, cb, cb); // reject("timeout"); if (getData) { return; } console.error("timeout", protocolId); WS.ins.onTimeout(); if (Date.now() - this._lastShowTimeoutDialogTime > 10000) { this._lastShowTimeoutDialogTime = Date.now(); } else { return; } TipsLayer.forceConfirm("Hello", "Welcome back!").then((success) => { if (success) { window.location.reload(); } }); }, timeout); }); } }