System.register(["cc"], function (_export, _context) { "use strict"; var _cclegacy, WsClient, _crd; _export("WsClient", void 0); return { setters: [function (_cc) { _cclegacy = _cc.cclegacy; }], execute: function () { _crd = true; _cclegacy._RF.push({}, "d14186TLCpDIa2nmf/PWkp9", "WsClient", undefined); /**web scoket 客户端*/ _export("WsClient", WsClient = class WsClient { onConnected(evt) {} onError(err) {} onClosing() {} onClosed(event) {} onMessage(msg) {} /** WebSocket对象*/ get ws() { return this._ws; } constructor(url, ca, autoConnect) { // this._ws = null; /** 连接地址*/ this._url = null; /**证书*/ this._ca = void 0; this._ca = ca; this._url = url; if (autoConnect) this.connect(); } /**连接成功时的回调 */ _onConnected(evt) { this.onConnected(evt); } /**收到消息时的回调 */ _onMessage(msg) { this.onMessage(msg); } /** 错误处理回调 */ _onError(err) { this.onError(err); } /**连接关闭时的回调 */ _onClosed(event) { this.onClosed(event); } /** * 获取当前连接状态 * @returns 是否处于活动状态 */ get isActive() { var _this$_ws; return ((_this$_ws = this._ws) == null ? void 0 : _this$_ws.readyState) === WebSocket.OPEN; } /** * 检查是否正在连接 * @returns 是否正在连接 */ get isConnecting() { var _this$_ws2; return ((_this$_ws2 = this._ws) == null ? void 0 : _this$_ws2.readyState) === WebSocket.CONNECTING; } /** *是否正在关闭 */ get isClosing() { var _this$_ws3; return ((_this$_ws3 = this._ws) == null ? void 0 : _this$_ws3.readyState) === WebSocket.CLOSING; } /**是否已经关闭 */ get isClosed() { var _this$_ws4; return ((_this$_ws4 = this._ws) == null ? void 0 : _this$_ws4.readyState) === WebSocket.CLOSED; } /**手动连接*/ connect() { if (this.isConnecting) return false; if (this.isActive) return false; const url = this._url; if (!url) return false; try { //eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore //eslint-disable-next-line @typescript-eslint/no-unsafe-call const ws = this._ca && url.startsWith('wss://') ? new WebSocket(url, {}, this._ca) : new WebSocket(url); ws.binaryType = 'arraybuffer'; ws.onmessage = this._onMessage.bind(this); ws.onopen = this._onConnected.bind(this); ws.onerror = this._onError.bind(this); ws.onclose = this._onClosed.bind(this); this._ws = ws; } catch (error) { this._onError(error instanceof Error ? error.message : 'Unknown error'); } return true; } /** * 主动关闭WebSocket连接 */ close(code, reason) { if (this.isClosed || this.isClosing) return; this.onClosing(); this._ws.close(code, reason); } /** * 发送数据 * @param data 指定格式数据 */ send(data) { this._ws.send(data); } }); _cclegacy._RF.pop(); _crd = false; } }; }); //# sourceMappingURL=3975fe5ce64c51324369e49e8afd9c685ef1036d.js.map