12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- 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());
- }
- public async syncRequest(
- protocolId: number,
- data: any,
- timeout: number = 8000
- ): 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;
- }
- WS.ins.reconnect();
- TipsLayer.show({
- title: "Hello",
- singleName: "timeout",
- content: "Welcome back!",
- forceConfirm: true,
- link: null,
- onConfirm: () => {
- // resolve(null);
- },
- });
- }, timeout);
- });
- }
- }
|