1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import { Hall } from "../../prefab/hall/Hall";
- 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<any> {
- 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);
- });
- }
- }
|