| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696 |
- System.register(["__unresolved_0", "cc", "__unresolved_1"], function (_export, _context) {
- "use strict";
- var _reporterNs, _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, error, warn, Logger, NetNode, _crd, NetNodeStateStrs, NetTipsType, NetNodeState;
- function _reportPossibleCrUseOfLogger(extras) {
- _reporterNs.report("Logger", "../../core/common/log/Logger", _context.meta, extras);
- }
- function _reportPossibleCrUseOfCallbackObject(extras) {
- _reporterNs.report("CallbackObject", "./NetInterface", _context.meta, extras);
- }
- function _reportPossibleCrUseOfINetworkTips(extras) {
- _reporterNs.report("INetworkTips", "./NetInterface", _context.meta, extras);
- }
- function _reportPossibleCrUseOfIProtocolHelper(extras) {
- _reporterNs.report("IProtocolHelper", "./NetInterface", _context.meta, extras);
- }
- function _reportPossibleCrUseOfIRequestProtocol(extras) {
- _reporterNs.report("IRequestProtocol", "./NetInterface", _context.meta, extras);
- }
- function _reportPossibleCrUseOfISocket(extras) {
- _reporterNs.report("ISocket", "./NetInterface", _context.meta, extras);
- }
- function _reportPossibleCrUseOfNetCallFunc(extras) {
- _reporterNs.report("NetCallFunc", "./NetInterface", _context.meta, extras);
- }
- function _reportPossibleCrUseOfNetData(extras) {
- _reporterNs.report("NetData", "./NetInterface", _context.meta, extras);
- }
- function _reportPossibleCrUseOfRequestObject(extras) {
- _reporterNs.report("RequestObject", "./NetInterface", _context.meta, extras);
- }
- _export("NetNode", void 0);
- return {
- setters: [function (_unresolved_) {
- _reporterNs = _unresolved_;
- }, function (_cc) {
- _cclegacy = _cc.cclegacy;
- __checkObsolete__ = _cc.__checkObsolete__;
- __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
- error = _cc.error;
- warn = _cc.warn;
- }, function (_unresolved_2) {
- Logger = _unresolved_2.Logger;
- }],
- execute: function () {
- _crd = true;
- _cclegacy._RF.push({}, "57f0fB90kNBUJ98yyu+jxjx", "NetNode", undefined);
- __checkObsolete__(['error', 'warn']);
- /*
- * CocosCreator网络节点基类,以及网络相关接口定义
- * 1. 网络连接、断开、请求发送、数据接收等基础功能
- * 2. 心跳机制
- * 3. 断线重连 + 请求重发
- * 4. 调用网络屏蔽层
- */
- NetNodeStateStrs = ["已关闭", "连接中", "验证中", "可传输数据"];
- /** 网络提示类型枚举 */
- _export("NetTipsType", NetTipsType = /*#__PURE__*/function (NetTipsType) {
- NetTipsType[NetTipsType["Connecting"] = 0] = "Connecting";
- NetTipsType[NetTipsType["ReConnecting"] = 1] = "ReConnecting";
- NetTipsType[NetTipsType["Requesting"] = 2] = "Requesting";
- return NetTipsType;
- }({}));
- /** 网络状态枚举 */
- _export("NetNodeState", NetNodeState = /*#__PURE__*/function (NetNodeState) {
- NetNodeState[NetNodeState["Closed"] = 0] = "Closed";
- NetNodeState[NetNodeState["Connecting"] = 1] = "Connecting";
- NetNodeState[NetNodeState["Checking"] = 2] = "Checking";
- NetNodeState[NetNodeState["Working"] = 3] = "Working";
- return NetNodeState;
- }({}));
- /** 网络连接参数 */
- /** 网络节点 */
- _export("NetNode", NetNode = class NetNode {
- constructor() {
- this._connectOptions = null;
- this._autoReconnect = 0;
- this._isSocketInit = false;
- // Socket是否初始化过
- this._isSocketOpen = false;
- // Socket是否连接成功过
- this._state = NetNodeState.Closed;
- // 节点当前状态
- this._socket = null;
- // Socket对象(可能是原生socket、websocket、wx.socket...)
- this._networkTips = null;
- // 网络提示ui对象(请求提示、断线重连提示等)
- this._protocolHelper = null;
- // 包解析对象
- this._connectedCallback = null;
- // 连接完成回调
- this._disconnectCallback = null;
- // 断线回调
- this._callbackExecuter = null;
- // 回调执行
- this._keepAliveTimer = null;
- // 心跳定时器
- this._receiveMsgTimer = null;
- // 接收数据定时器
- this._reconnectTimer = null;
- // 重连定时器
- this._heartTime = 10000;
- // 心跳间隔
- this._receiveTime = 6000000;
- // 多久没收到数据断开
- this._reconnetTimeOut = 8000000;
- // 重连间隔
- this._requests = Array();
- // 请求列表
- this._listener = {};
- }
- // 监听者列表
- /********************** 网络相关处理 *********************/
- init(socket, protocol, networkTips, execFunc) {
- if (networkTips === void 0) {
- networkTips = null;
- }
- if (execFunc === void 0) {
- execFunc = null;
- }
- (_crd && Logger === void 0 ? (_reportPossibleCrUseOfLogger({
- error: Error()
- }), Logger) : Logger).logNet("\u7F51\u7EDC\u521D\u59CB\u5316");
- this._socket = socket;
- this._protocolHelper = protocol;
- this._networkTips = networkTips;
- this._callbackExecuter = execFunc ? execFunc : (callback, buffer) => {
- callback.callback.call(callback.target, buffer);
- };
- }
- /**
- * 请求连接服务器
- * @param options 连接参数
- */
- connect(options) {
- if (this._socket && this._state == NetNodeState.Closed) {
- if (!this._isSocketInit) {
- this.initSocket();
- }
- this._state = NetNodeState.Connecting;
- if (!this._socket.connect(options)) {
- this.updateNetTips(NetTipsType.Connecting, false);
- return false;
- }
- if (this._connectOptions == null && typeof options.autoReconnect == "number") {
- this._autoReconnect = options.autoReconnect;
- }
- this._connectOptions = options;
- this.updateNetTips(NetTipsType.Connecting, true);
- return true;
- }
- return false;
- }
- initSocket() {
- if (this._socket) {
- this._socket.onConnected = event => {
- this.onConnected(event);
- };
- this._socket.onMessage = msg => {
- this.onMessage(msg);
- };
- this._socket.onError = event => {
- this.onError(event);
- };
- this._socket.onClosed = event => {
- this.onClosed(event);
- };
- this._isSocketInit = true;
- }
- }
- updateNetTips(tipsType, isShow) {
- if (this._networkTips) {
- if (tipsType == NetTipsType.Requesting) {
- this._networkTips.requestTips(isShow);
- } else if (tipsType == NetTipsType.Connecting) {
- this._networkTips.connectTips(isShow);
- } else if (tipsType == NetTipsType.ReConnecting) {
- this._networkTips.reconnectTips(isShow);
- }
- }
- }
- /** 网络连接成功 */
- onConnected(event) {
- (_crd && Logger === void 0 ? (_reportPossibleCrUseOfLogger({
- error: Error()
- }), Logger) : Logger).logNet("网络已连接");
- this._isSocketOpen = true; // 如果设置了鉴权回调,在连接完成后进入鉴权阶段,等待鉴权结束
- if (this._connectedCallback !== null) {
- this._state = NetNodeState.Checking;
- this._connectedCallback(() => {
- this.onChecked();
- });
- } else {
- this.onChecked();
- }
- (_crd && Logger === void 0 ? (_reportPossibleCrUseOfLogger({
- error: Error()
- }), Logger) : Logger).logNet("\u7F51\u7EDC\u5DF2\u8FDE\u63A5\u5F53\u524D\u72B6\u6001\u4E3A\u3010" + NetNodeStateStrs[this._state] + "\u3011");
- }
- /** 连接验证成功,进入工作状态 */
- onChecked() {
- (_crd && Logger === void 0 ? (_reportPossibleCrUseOfLogger({
- error: Error()
- }), Logger) : Logger).logNet("连接验证成功,进入工作状态");
- this._state = NetNodeState.Working; // 关闭连接或重连中的状态显示
- this.updateNetTips(NetTipsType.Connecting, false);
- this.updateNetTips(NetTipsType.ReConnecting, false); // 重发待发送信息
- var requests = this._requests.concat();
- if (requests.length > 0) {
- (_crd && Logger === void 0 ? (_reportPossibleCrUseOfLogger({
- error: Error()
- }), Logger) : Logger).logNet("\u8BF7\u6C42\u3010" + this._requests.length + "\u3011\u4E2A\u5F85\u53D1\u9001\u7684\u4FE1\u606F");
- for (var i = 0; i < requests.length;) {
- var req = requests[i];
- this._socket.send(req.buffer);
- if (req.rspObject == null || req.rspCmd != "") {
- requests.splice(i, 1);
- } else {
- ++i;
- }
- } // 如果还有等待返回的请求,启动网络请求层
- this.updateNetTips(NetTipsType.Requesting, this._requests.length > 0);
- }
- }
- /** 接收到一个完整的消息包 */
- onMessage(msg) {
- // Logger.logNet(`接受消息状态为【${NetNodeStateStrs[this._state]}】`);
- var json = JSON.parse(msg); // 进行头部的校验(实际包长与头部长度是否匹配)
- if (!this._protocolHelper.checkResponsePackage(json)) {
- error("\u6821\u9A8C\u63A5\u53D7\u6D88\u606F\u6570\u636E\u5F02\u5E38");
- return;
- } // 处理相应包数据
- if (!this._protocolHelper.handlerResponsePackage(json)) {
- if (this._networkTips) this._networkTips.responseErrorCode(json.code);
- } // 接受到数据,重新定时收数据计时器
- this.resetReceiveMsgTimer(); // 重置心跳包发送器
- this.resetHearbeatTimer(); // 触发消息执行
- var rspCmd = this._protocolHelper.getPackageId(json);
- (_crd && Logger === void 0 ? (_reportPossibleCrUseOfLogger({
- error: Error()
- }), Logger) : Logger).logNet("\u63A5\u53D7\u5230\u547D\u4EE4\u3010" + rspCmd + "\u3011\u7684\u6D88\u606F"); // 优先触发request队列
- if (this._requests.length > 0) {
- for (var reqIdx in this._requests) {
- var req = this._requests[reqIdx];
- if (req.rspCmd == rspCmd && req.rspObject) {
- (_crd && Logger === void 0 ? (_reportPossibleCrUseOfLogger({
- error: Error()
- }), Logger) : Logger).logNet("\u89E6\u53D1\u8BF7\u6C42\u547D\u4EE4\u3010" + rspCmd + "\u3011\u7684\u56DE\u8C03");
- this._callbackExecuter(req.rspObject, json.data);
- this._requests.splice(parseInt(reqIdx), 1);
- break;
- }
- }
- if (this._requests.length == 0) {
- this.updateNetTips(NetTipsType.Requesting, false);
- } else {
- (_crd && Logger === void 0 ? (_reportPossibleCrUseOfLogger({
- error: Error()
- }), Logger) : Logger).logNet("\u8BF7\u6C42\u961F\u5217\u4E2D\u8FD8\u6709\u3010" + this._requests.length + "\u3011\u4E2A\u8BF7\u6C42\u5728\u7B49\u5F85");
- }
- }
- var listeners = this._listener[rspCmd];
- if (null != listeners) {
- for (var rsp of listeners) {
- (_crd && Logger === void 0 ? (_reportPossibleCrUseOfLogger({
- error: Error()
- }), Logger) : Logger).logNet("\u89E6\u53D1\u76D1\u542C\u547D\u4EE4\u3010" + rspCmd + "\u3011\u7684\u56DE\u8C03");
- this._callbackExecuter(rsp, json.data);
- }
- }
- }
- onError(event) {
- error(event);
- }
- onClosed(event) {
- this.clearTimer(); // 执行断线回调,返回false表示不进行重连
- if (this._disconnectCallback && !this._disconnectCallback()) {
- (_crd && Logger === void 0 ? (_reportPossibleCrUseOfLogger({
- error: Error()
- }), Logger) : Logger).logNet("\u65AD\u5F00\u8FDE\u63A5");
- return;
- } // 自动重连
- if (this.isAutoReconnect()) {
- this.updateNetTips(NetTipsType.ReConnecting, true);
- this._reconnectTimer = setTimeout(() => {
- this._socket.close();
- this._state = NetNodeState.Closed;
- this.connect(this._connectOptions);
- if (this._autoReconnect > 0) {
- this._autoReconnect -= 1;
- }
- }, this._reconnetTimeOut);
- } else {
- this._state = NetNodeState.Closed;
- }
- }
- /**
- * 断开网络
- * @param code 关闭码
- * @param reason 关闭原因
- */
- close(code, reason) {
- this.clearTimer();
- this._listener = {};
- this._requests.length = 0;
- if (this._networkTips) {
- this._networkTips.connectTips(false);
- this._networkTips.reconnectTips(false);
- this._networkTips.requestTips(false);
- }
- if (this._socket) {
- this._socket.close(code, reason);
- } else {
- this._state = NetNodeState.Closed;
- }
- }
- /**
- * 只是关闭Socket套接字(仍然重用缓存与当前状态)
- * @param code 关闭码
- * @param reason 关闭原因
- */
- closeSocket(code, reason) {
- if (this._socket) {
- this._socket.close(code, reason);
- }
- }
- /**
- * 发起请求,如果当前处于重连中,进入缓存列表等待重连完成后发送
- * @param buf 网络数据
- * @param force 是否强制发送
- */
- send(buf, force) {
- if (force === void 0) {
- force = false;
- }
- if (this._state == NetNodeState.Working || force) {
- return this._socket.send(buf);
- } else if (this._state == NetNodeState.Checking || this._state == NetNodeState.Connecting) {
- this._requests.push({
- buffer: buf,
- rspCmd: "",
- rspObject: null
- });
- (_crd && Logger === void 0 ? (_reportPossibleCrUseOfLogger({
- error: Error()
- }), Logger) : Logger).logNet("\u5F53\u524D\u72B6\u6001\u4E3A\u3010" + NetNodeStateStrs[this._state] + "\u3011,\u7E41\u5FD9\u5E76\u7F13\u51B2\u53D1\u9001\u6570\u636E");
- return 0;
- } else {
- error("\u5F53\u524D\u72B6\u6001\u4E3A\u3010" + NetNodeStateStrs[this._state] + "\u3011,\u8BF7\u6C42\u9519\u8BEF");
- return -1;
- }
- }
- /**
- * 发起请求,并进入缓存列表
- * @param reqProtocol 请求协议
- * @param rspObject 回调对象
- * @param showTips 是否触发请求提示
- * @param force 是否强制发送
- */
- request(reqProtocol, rspObject, showTips, force) {
- if (showTips === void 0) {
- showTips = true;
- }
- if (force === void 0) {
- force = false;
- }
- var rspCmd = this._protocolHelper.handlerRequestPackage(reqProtocol);
- this.base_request(reqProtocol, rspCmd, rspObject, showTips, force);
- }
- /**
- * 唯一request,确保没有同一响应的请求(避免一个请求重复发送,netTips界面的屏蔽也是一个好的方法)
- * @param reqProtocol 请求协议
- * @param rspObject 回调对象
- * @param showTips 是否触发请求提示
- * @param force 是否强制发送
- */
- requestUnique(reqProtocol, rspObject, showTips, force) {
- if (showTips === void 0) {
- showTips = true;
- }
- if (force === void 0) {
- force = false;
- }
- var rspCmd = this._protocolHelper.handlerRequestPackage(reqProtocol);
- for (var i = 0; i < this._requests.length; ++i) {
- if (this._requests[i].rspCmd == rspCmd) {
- (_crd && Logger === void 0 ? (_reportPossibleCrUseOfLogger({
- error: Error()
- }), Logger) : Logger).logNet("\u547D\u4EE4\u3010" + rspCmd + "\u3011\u91CD\u590D\u8BF7\u6C42");
- return false;
- }
- }
- this.base_request(reqProtocol, rspCmd, rspObject, showTips, force);
- return true;
- }
- base_request(reqProtocol, rspCmd, rspObject, showTips, force) {
- if (showTips === void 0) {
- showTips = true;
- }
- if (force === void 0) {
- force = false;
- }
- var buf = JSON.stringify(reqProtocol); // 转为二进制流发送
- if (this._state == NetNodeState.Working || force) {
- this._socket.send(buf);
- }
- (_crd && Logger === void 0 ? (_reportPossibleCrUseOfLogger({
- error: Error()
- }), Logger) : Logger).logNet("\u961F\u5217\u547D\u4EE4\u4E3A\u3010" + rspCmd + "\u3011\u7684\u8BF7\u6C42\uFF0C\u7B49\u5F85\u8BF7\u6C42\u6570\u636E\u7684\u56DE\u8C03"); // 进入发送缓存列表
- this._requests.push({
- buffer: buf,
- rspCmd,
- rspObject
- }); // 启动网络请求层
- if (showTips) {
- this.updateNetTips(NetTipsType.Requesting, true);
- }
- }
- /********************** 回调相关处理 *********************/
- /**
- * 设置一个唯一的服务器推送监听
- * @param cmd 命令字串
- * @param callback 回调方法
- * @param target 目标对象
- */
- setResponeHandler(cmd, callback, target) {
- if (callback == null) {
- error("\u547D\u4EE4\u4E3A\u3010" + cmd + "\u3011\u8BBE\u7F6E\u54CD\u5E94\u5904\u7406\u7A0B\u5E8F\u9519\u8BEF");
- return false;
- }
- this._listener[cmd] = [{
- target,
- callback
- }];
- return true;
- }
- /**
- * 可添加多个同类返回消息的监听
- * @param cmd 命令字串
- * @param callback 回调方法
- * @param target 目标对象
- * @returns
- */
- addResponeHandler(cmd, callback, target) {
- if (callback == null) {
- error("\u547D\u4EE4\u4E3A\u3010" + cmd + "\u3011\u6DFB\u52A0\u54CD\u5E94\u5904\u7406\u7A0B\u5E8F\u9519\u8BEF");
- return false;
- }
- var rspObject = {
- target,
- callback
- };
- if (null == this._listener[cmd]) {
- this._listener[cmd] = [rspObject];
- } else {
- var index = this.getNetListenersIndex(cmd, rspObject);
- if (-1 == index) {
- this._listener[cmd].push(rspObject);
- }
- }
- return true;
- }
- /**
- * 删除一个监听中指定子回调
- * @param cmd 命令字串
- * @param callback 回调方法
- * @param target 目标对象
- */
- removeResponeHandler(cmd, callback, target) {
- if (null != this._listener[cmd] && callback != null) {
- var index = this.getNetListenersIndex(cmd, {
- target,
- callback
- });
- if (-1 != index) {
- this._listener[cmd].splice(index, 1);
- }
- }
- }
- /**
- * 清除所有监听或指定命令的监听
- * @param cmd 命令字串(默认不填为清除所有)
- */
- cleanListeners(cmd) {
- if (cmd === void 0) {
- cmd = "";
- }
- if (cmd == "") {
- this._listener = {};
- } else {
- delete this._listener[cmd];
- }
- }
- getNetListenersIndex(cmd, rspObject) {
- var index = -1;
- for (var i = 0; i < this._listener[cmd].length; i++) {
- var iterator = this._listener[cmd][i];
- if (iterator.callback == rspObject.callback && iterator.target == rspObject.target) {
- index = i;
- break;
- }
- }
- return index;
- }
- /********************** 心跳、超时相关处理 *********************/
- resetReceiveMsgTimer() {
- if (this._receiveMsgTimer !== null) {
- clearTimeout(this._receiveMsgTimer);
- }
- this._receiveMsgTimer = setTimeout(() => {
- warn("接收消息定时器关闭网络连接");
- this._socket.close();
- }, this._receiveTime);
- }
- resetHearbeatTimer() {
- if (this._keepAliveTimer !== null) {
- clearTimeout(this._keepAliveTimer);
- }
- this._keepAliveTimer = setTimeout(() => {
- (_crd && Logger === void 0 ? (_reportPossibleCrUseOfLogger({
- error: Error()
- }), Logger) : Logger).logNet("网络节点保持活跃发送心跳信息");
- this.send(this._protocolHelper.getHearbeat());
- }, this._heartTime);
- }
- clearTimer() {
- if (this._receiveMsgTimer !== null) {
- clearTimeout(this._receiveMsgTimer);
- }
- if (this._keepAliveTimer !== null) {
- clearTimeout(this._keepAliveTimer);
- }
- if (this._reconnectTimer !== null) {
- clearTimeout(this._reconnectTimer);
- }
- }
- /** 是否自动重连接 */
- isAutoReconnect() {
- return this._autoReconnect != 0;
- }
- /** 拒绝重新连接 */
- rejectReconnect() {
- this._autoReconnect = 0;
- this.clearTimer();
- }
- });
- _cclegacy._RF.pop();
- _crd = false;
- }
- };
- });
- //# sourceMappingURL=744865273d49a9739121b97ff2e12db7ab983596.js.map
|