WsM.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { Hall } from "../../prefab/hall/Hall";
  2. import { TipsLayer } from "../../prefab/layer/TipsLayer";
  3. import { Tips } from "../mgr/Tips";
  4. import { WS, WsRequest, WsResponse } from "../net/WS";
  5. export default class WsM {
  6. private static _ins: WsM;
  7. public static get ins(): WsM {
  8. return (WsM._ins ??= new WsM());
  9. }
  10. private _lastShowTimeoutDialogTime: number = 0;
  11. public async syncRequest(
  12. protocolId: number,
  13. data: any,
  14. timeout: number = 5000
  15. ): Promise<any> {
  16. return new Promise((resolve, reject) => {
  17. let getData = false;
  18. let cb = (msg: WsResponse) => {
  19. getData = true;
  20. console.log("websocket msg", msg);
  21. WS.ins.off(protocolId, cb, cb);
  22. if (msg.success == true) {
  23. if (msg.data) {
  24. resolve(msg.data);
  25. } else {
  26. resolve({});
  27. }
  28. } else {
  29. console.error(msg);
  30. Tips.show(msg.msg);
  31. resolve(null);
  32. }
  33. };
  34. WS.ins.on(protocolId, cb, cb);
  35. WsRequest.create(protocolId).setData(data).send();
  36. setTimeout(() => {
  37. // WS.ins.off(protocolId, cb, cb);
  38. // reject("timeout");
  39. if (getData) {
  40. return;
  41. }
  42. console.error("timeout", protocolId);
  43. WS.ins.onTimeout();
  44. if (Date.now() - this._lastShowTimeoutDialogTime > 10000) {
  45. this._lastShowTimeoutDialogTime = Date.now();
  46. } else {
  47. return;
  48. }
  49. TipsLayer.forceConfirm("Hello", "Welcome back!").then((success) => {
  50. if (success) {
  51. window.location.reload();
  52. }
  53. });
  54. }, timeout);
  55. });
  56. }
  57. }