"use strict"; cc._RF.push(module, '2a0acyTLi1LGLc48IS9WAN+', 'bridge.connect'); // Scripts/zbridge/cmps/bridge.connect.js "use strict"; /****************************************************************** * * 桥接网络连接 * ******************************************************************/ var BridgeComponent = require("bridge.component"); var LoginMod = require("login.mod"); cc.Class({ "extends": BridgeComponent, properties: { silentTimes: { "default": 2, displayName: "静默连接次数" } }, // 载入 onLoad: function onLoad() { // 当前次数 this.times = 0; // 视图监听 this.vbind([["connFlag", this.onConnectionChanged.bind(this)]]); nx.bridge.connector = this; }, // 网络连接状态改变 onConnectionChanged: function onConnectionChanged(_cur, _old) { if (_cur == _old) { return; } switch (_cur) { // 连接开始 case "linking": { nx.info("$Connect: 开始连接..."); nx.bridge.addWaiting("WaitConnecting"); } break; // 连接完成 case "good": { nx.info("$Connect: 连接完成"); nx.bridge.delWaiting("WaitConnecting"); this.times = 0; } break; case "close": { nx.info("$Connect: 连接关闭"); nx.bridge.delWaiting("WaitConnecting"); this.onDisconnected(); } break; default: break; } }, // 静默重试 autoTry: function autoTry() { var LC = LoginMod.getInstance(); // 重新同步数据 var refetch = function refetch(_ret, _data) { if (!_ret) { return; } nx.bridge.game.initModules(); }; // 0-登录失败 // 1-登录成功 // 2-账号异常 // 4-服务器维护中 LC.autoEnter(function (_ret, _data, _code) { // 失败处理 if (!_ret) { var msg = _data; if (nx.dt.numGood(_code)) { msg = "ErrAccount" + _code; } nx.mbox(msg, ['confirm'], function (_key, _box) { nx.restart(); }, "LOGIN_FAILED"); return; } // 准备重新同步数据 nx.tbox("ReLoginSuccess"); nx.bridge.game.gameReady(refetch); }, true); }, // 网络断开 onDisconnected: function onDisconnected() { var _this = this; // 次数过多 this.times += 1; if (this.times > this.silentTimes) { nx.mbox("AskReconnect", ['quit', 'retry'], function (_key, _box) { _box.close(); if (_key == 'quit') { nx.restart(); return; } _this.times = 0; _this.autoTry(); }); return; } // 静默连接 this.autoTry(); } }); cc._RF.pop();