Files
fc/dev/project/assets/Scripts/zbridge/cmps/bridge.connect.js
T
2026-05-23 22:10:14 +08:00

134 lines
3.0 KiB
JavaScript

/******************************************************************
*
* 桥接网络连接
*
******************************************************************/
const BridgeComponent = require( "bridge.component" );
const LoginMod = require( "login.mod" );
cc.Class( {
extends: BridgeComponent,
properties: {
silentTimes: { default: 2, displayName: "静默连接次数" },
},
// 载入
onLoad: function() {
// 当前次数
this.times = 0;
// 视图监听
this.vbind( [
["connFlag", this.onConnectionChanged.bind( this ) ],
] );
nx.bridge.connector = this;
},
// 网络连接状态改变
onConnectionChanged: function( _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() {
let LC = LoginMod.getInstance();
// 重新同步数据
let refetch = function( _ret, _data ) {
if( !_ret ) {
return;
}
nx.bridge.game.initModules();
};
// 0-登录失败
// 1-登录成功
// 2-账号异常
// 4-服务器维护中
LC.autoEnter( ( _ret, _data, _code ) => {
// 失败处理
if( !_ret ) {
let msg = _data;
if( nx.dt.numGood( _code ) ) {
msg = "ErrAccount" + _code;
}
nx.mbox( msg, ['confirm'], ( _key, _box ) => {
nx.restart();
}, "LOGIN_FAILED" );
return;
}
// 准备重新同步数据
nx.tbox( "ReLoginSuccess" );
nx.bridge.game.gameReady( refetch );
}, true );
},
// 网络断开
onDisconnected: function() {
// 次数过多
this.times += 1;
if( this.times > this.silentTimes ) {
nx.mbox( "AskReconnect", [ 'quit', 'retry' ], ( _key, _box ) => {
_box.close();
if( _key == 'quit' ) {
nx.restart();
return;
}
this.times = 0;
this.autoTry();
} );
return;
}
// 静默连接
this.autoTry();
},
} );