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

269 lines
6.7 KiB
JavaScript

/*-----------------------------------------------------+
* 桥接: 控制器基类
+-----------------------------------------------------*/
const BridgeController = cc.Class( {
name: "BridgeController",
statics: {
instance: null,
// 实例化单利
getInstance: function( _id, _key, _rebuild = false ) {
// 强制重建
if( _rebuild && this.instance ) {
this.instance.onDestroy();
delete this.instance;
this.instance = null;
}
if( this.instance ) {
return this.instance;
}
new this( );
this.instance.init( _id, _key);
return this.instance;
},
},
// 构造
ctor: function() {
if( this.constructor.instance ) {
throw new Error( "不能重复实例化一个单例" );
}
// 桥接:内部使用
this.__fid = 0;
this.__fkey = "";
this.__flock = true;
// 监听回调
this.hooks = {};
this.constructor.instance = this;
},
// 销毁
onDestroy: function() {
// 注销网络协议监听
for( let id in this.hooks ) {
client.socket.unbindCmd( id, this.__fkey );
}
this.hooks = {};
},
// 初始化
init: function( _id, _key ) {
// 桥接:内部使用
this.__fid = _id || 0;
this.__fkey = _key || "";
// 初始化配置
this.initConfig();
// 本地事件监听
this.registerEvents();
// 协议监听
this.registerProtocals();
},
// ============================================================
// 配置&&监听
// ============================================================
// 初始化配置数据
initConfig: function() {
// 重载 TODO...
},
// 注册监听事件
registerEvents: function() {
// 重载 TODO...
},
// 注册协议接受事件
registerProtocals: function() {
// 重载 TODO...
},
// ============================================================
// 初始化/解锁相关
// ============================================================
// 设置功能ID
setFunctionID: function( _id, _key ) {
this.__fid = _id;
this.__fkey = _key;
},
// 设置功能锁
setFunctionLock: function( _lock ) {
this.__flock = !!_lock;
},
// 从服务器初始化数据
reqBaseFromServer: function( _cb ) {
// 重载 TODO...
nx.dt.fnInvoke( _cb, true );
},
// 配置队列加载
loadConfigs: function( _lst, _cb ) {
// 判空
if( nx.dt.arrEmpty( _lst ) ) {
nx.dt.fnInvoke( _cb, true );
return;
}
// 依次加载
let files = nx.dt.objClone( _lst );
let next = function() {
if( nx.dt.arrEmpty( files ) ) {
nx.dt.fnInvoke( _cb, true );
return;
}
let key = files.shift();
if( game.configs[key] ) {
next();
return;
}
let path = cc.path.join( "configs/excels", key );
nx.res.loadJson( path, ( _err, _data ) => {
if( _err ) {
nx.error( "$配置加载失败:", path );
} else {
game.configs[key] = _data;
}
next();
} );
};
next();
},
// ============================================================
// 协议相关
// ============================================================
// 消息监听
RegisterProtocal: function( cmd, func ) {
// 参数无效
if( !cmd || typeof ( func ) != "function" ) {
cc.error( "消息监听失败:" + cmd );
return;
}
// 重复
if( this.hooks[ cmd ] ) {
cc.error( "消息监听重复:" + cmd );
return;
}
// 老方法
this.hooks[ cmd ] = {
fnRegist: func, // 老监听回调,通过RegisterProtocal
fnSend: null, // QA监听回调,通过SendProtocal
};
// 走新方法
client.socket.bindCmd( cmd, this.onRecvProtocal.bind( this ), this.__fkey );
},
// 消息发送
SendProtocal: function( cmd, data, _cb ) {
// 参数无效
if( !cmd ) {
cc.error( "发送失败,错误的协议号" );
return;
}
// QA监听回调
if( this.hooks[ cmd ] && ( typeof ( _cb ) == "function" ) ) {
this.hooks[ cmd ].fnSend = _cb;
}
// 发送
client.socket.send( cmd, data || {} );
},
// 消息接收
onRecvProtocal: function( _data, _cmd ) {
let hook = this.hooks[ _cmd ];
if( !hook ) {
cc.error( "接收到未注册消息,什么鬼:", _cmd );
return;
}
// 两种监听回调
try {
if( typeof ( hook.fnRegist ) == "function" ) {
// hook.fnRegist( _data );
hook.fnRegist.call( this, _data );
}
// 单次调用后解除回调
if( typeof ( hook.fnSend ) == "function" ) {
// code如果 等于1则为成功,否则为失败
if( nx.dt.numGood( _data.code ) ) {
if( _data.code == 1 ) {
hook.fnSend( true, _data );
} else {
hook.fnSend( false, _data.msg || "ErrFailed", _data.code );
}
hook.fnSend = null;
return;
}
// 如果code不存在
else if( nx.dt.strEmpty( _data.msg ) ) {
hook.fnSend( true, _data );
}
else {
hook.fnSend( false, _data.msg, _data.code );
}
hook.fnSend = null;
}
}
catch( err ) {
cc.error( "消息接收后处理失败:" + _cmd + " msg:" + err.message, err.stack );
if( err && cc.sys.isNative && window && window.__errorHandler ) {
window.__errorHandler( err.message, "", "", err.stack );
}
}
},
// 成功的返回
// 新定义的规则: _data.msg 空/非空 == 成功/失败
isGoodData: function( _data, _tbox = true ) {
let ok = _data && ( _data.code == 1 || nx.dt.strEmpty( _data.msg ) );
if( !ok && _tbox ) {
nx.tbox( nx.text.getKey( _data.msg ) );
}
return ok;
},
} );
module.exports = BridgeController;