3975fe5ce64c51324369e49e8afd9c685ef1036d.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, WsClient, _crd;
  4. _export("WsClient", void 0);
  5. return {
  6. setters: [function (_cc) {
  7. _cclegacy = _cc.cclegacy;
  8. }],
  9. execute: function () {
  10. _crd = true;
  11. _cclegacy._RF.push({}, "d14186TLCpDIa2nmf/PWkp9", "WsClient", undefined);
  12. /**web scoket 客户端*/
  13. _export("WsClient", WsClient = class WsClient {
  14. onConnected(evt) {}
  15. onError(err) {}
  16. onClosing() {}
  17. onClosed(event) {}
  18. onMessage(msg) {}
  19. /** WebSocket对象*/
  20. get ws() {
  21. return this._ws;
  22. }
  23. constructor(url, ca, autoConnect) {
  24. //
  25. this._ws = null;
  26. /** 连接地址*/
  27. this._url = null;
  28. /**证书*/
  29. this._ca = void 0;
  30. this._ca = ca;
  31. this._url = url;
  32. if (autoConnect) this.connect();
  33. }
  34. /**连接成功时的回调 */
  35. _onConnected(evt) {
  36. this.onConnected(evt);
  37. }
  38. /**收到消息时的回调 */
  39. _onMessage(msg) {
  40. this.onMessage(msg);
  41. }
  42. /** 错误处理回调 */
  43. _onError(err) {
  44. this.onError(err);
  45. }
  46. /**连接关闭时的回调 */
  47. _onClosed(event) {
  48. this.onClosed(event);
  49. }
  50. /**
  51. * 获取当前连接状态
  52. * @returns 是否处于活动状态
  53. */
  54. get isActive() {
  55. var _this$_ws;
  56. return ((_this$_ws = this._ws) == null ? void 0 : _this$_ws.readyState) === WebSocket.OPEN;
  57. }
  58. /**
  59. * 检查是否正在连接
  60. * @returns 是否正在连接
  61. */
  62. get isConnecting() {
  63. var _this$_ws2;
  64. return ((_this$_ws2 = this._ws) == null ? void 0 : _this$_ws2.readyState) === WebSocket.CONNECTING;
  65. }
  66. /**
  67. *是否正在关闭
  68. */
  69. get isClosing() {
  70. var _this$_ws3;
  71. return ((_this$_ws3 = this._ws) == null ? void 0 : _this$_ws3.readyState) === WebSocket.CLOSING;
  72. }
  73. /**是否已经关闭 */
  74. get isClosed() {
  75. var _this$_ws4;
  76. return ((_this$_ws4 = this._ws) == null ? void 0 : _this$_ws4.readyState) === WebSocket.CLOSED;
  77. }
  78. /**手动连接*/
  79. connect() {
  80. if (this.isConnecting) return false;
  81. if (this.isActive) return false;
  82. var url = this._url;
  83. if (!url) return false;
  84. try {
  85. //eslint-disable-next-line @typescript-eslint/ban-ts-comment
  86. //@ts-ignore
  87. //eslint-disable-next-line @typescript-eslint/no-unsafe-call
  88. var ws = this._ca && url.startsWith('wss://') ? new WebSocket(url, {}, this._ca) : new WebSocket(url);
  89. ws.binaryType = 'arraybuffer';
  90. ws.onmessage = this._onMessage.bind(this);
  91. ws.onopen = this._onConnected.bind(this);
  92. ws.onerror = this._onError.bind(this);
  93. ws.onclose = this._onClosed.bind(this);
  94. this._ws = ws;
  95. } catch (error) {
  96. this._onError(error instanceof Error ? error.message : 'Unknown error');
  97. }
  98. return true;
  99. }
  100. /**
  101. * 主动关闭WebSocket连接
  102. */
  103. close(code, reason) {
  104. if (this.isClosed || this.isClosing) return;
  105. this.onClosing();
  106. this._ws.close(code, reason);
  107. }
  108. /**
  109. * 发送数据
  110. * @param data 指定格式数据
  111. */
  112. send(data) {
  113. this._ws.send(data);
  114. }
  115. });
  116. _cclegacy._RF.pop();
  117. _crd = false;
  118. }
  119. };
  120. });
  121. //# sourceMappingURL=3975fe5ce64c51324369e49e8afd9c685ef1036d.js.map