123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- 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);
- });
- }
- }
|