| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- System.register(["__unresolved_0", "cc", "__unresolved_1"], function (_export, _context) {
- "use strict";
- var _reporterNs, _cclegacy, Logger, WebSock, _crd;
- function _reportPossibleCrUseOfLogger(extras) {
- _reporterNs.report("Logger", "../../core/common/log/Logger", _context.meta, extras);
- }
- function _reportPossibleCrUseOfISocket(extras) {
- _reporterNs.report("ISocket", "./NetInterface", _context.meta, extras);
- }
- function _reportPossibleCrUseOfMessageFunc(extras) {
- _reporterNs.report("MessageFunc", "./NetInterface", _context.meta, extras);
- }
- function _reportPossibleCrUseOfNetData(extras) {
- _reporterNs.report("NetData", "./NetInterface", _context.meta, extras);
- }
- _export("WebSock", void 0);
- return {
- setters: [function (_unresolved_) {
- _reporterNs = _unresolved_;
- }, function (_cc) {
- _cclegacy = _cc.cclegacy;
- }, function (_unresolved_2) {
- Logger = _unresolved_2.Logger;
- }],
- execute: function () {
- _crd = true;
- _cclegacy._RF.push({}, "70df2VbIU9B66Fr+op8FKJp", "WebSock", undefined);
- /*
- * @Author: dgflash
- * @Date: 2021-07-03 16:13:17
- * @LastEditors: dgflash
- * @LastEditTime: 2022-09-09 17:42:19
- */
- /**
- * WebSocket 封装
- * 1. 连接/断开相关接口
- * 2. 网络异常回调
- * 3. 数据发送与接收
- */
- _export("WebSock", WebSock = class WebSock {
- constructor() {
- this._ws = null;
- // websocket对象
- /** 网络连接成功事件 */
- this.onConnected = null;
- /** 接受到网络数据事件 */
- this.onMessage = null;
- /** 网络错误事件 */
- this.onError = null;
- /** 网络断开事件 */
- this.onClosed = null;
- }
- /** 请求连接 */
- connect(options) {
- if (this._ws) {
- if (this._ws.readyState === WebSocket.CONNECTING) {
- (_crd && Logger === void 0 ? (_reportPossibleCrUseOfLogger({
- error: Error()
- }), Logger) : Logger).logNet("websocket connecting, wait for a moment...");
- return false;
- }
- }
- let url = null;
- if (options.url) {
- url = options.url;
- } else {
- let ip = options.ip;
- let port = options.port;
- let protocol = options.protocol;
- url = `${protocol}://${ip}:${port}`;
- }
- this._ws = new WebSocket(url);
- this._ws.binaryType = options.binaryType ? options.binaryType : "arraybuffer";
- this._ws.onmessage = event => {
- let onMessage = this.onMessage;
- onMessage(event.data);
- };
- this._ws.onopen = this.onConnected;
- this._ws.onerror = this.onError;
- this._ws.onclose = this.onClosed;
- return true;
- }
- /**
- * 发送数据
- * @param buffer 网络数据
- */
- send(buffer) {
- if (this._ws && this._ws.readyState == WebSocket.OPEN) {
- this._ws.send(buffer);
- return 1;
- }
- return -1;
- }
- /**
- * 网络断开
- * @param code 关闭码
- * @param reason 关闭原因
- */
- close(code, reason) {
- if (this._ws) {
- this._ws.close(code, reason);
- }
- }
- });
- _cclegacy._RF.pop();
- _crd = false;
- }
- };
- });
- //# sourceMappingURL=a398b03a98801c1674542f8831e019207241c4be.js.map
|