216 lines
5.1 KiB
JavaScript
216 lines
5.1 KiB
JavaScript
|
|
/*******************************************************************************
|
||
|
|
*
|
||
|
|
* Nx WEB通信
|
||
|
|
*
|
||
|
|
*
|
||
|
|
*
|
||
|
|
* 2021.12.10
|
||
|
|
******************************************************************************/
|
||
|
|
|
||
|
|
const ERR = require( "error" );
|
||
|
|
const NxObject = require( "nx.object" );
|
||
|
|
|
||
|
|
let NxWebManager = cc.Class({
|
||
|
|
|
||
|
|
extends : NxObject,
|
||
|
|
name : "NxWebManager",
|
||
|
|
|
||
|
|
// 初始化
|
||
|
|
initialize: function( _args ) {
|
||
|
|
|
||
|
|
// USPER
|
||
|
|
if( !this._super( _args ) ) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
this.url = ""; // 服务器地址
|
||
|
|
this.token = ""; // 令牌
|
||
|
|
// this.taskDB = {}; // 任务表
|
||
|
|
|
||
|
|
// 全局服务
|
||
|
|
nx.web = this;
|
||
|
|
|
||
|
|
return true;
|
||
|
|
},
|
||
|
|
|
||
|
|
// 销毁
|
||
|
|
uninitialize: function() {
|
||
|
|
|
||
|
|
this.clean();
|
||
|
|
nx.web = null;
|
||
|
|
|
||
|
|
// USPER
|
||
|
|
return this._super();
|
||
|
|
},
|
||
|
|
|
||
|
|
// 清理
|
||
|
|
clean: function() {
|
||
|
|
|
||
|
|
// for( let i in this.taskDB ) {
|
||
|
|
// nx.factory.remove( this.taskDB[i] );
|
||
|
|
// }
|
||
|
|
// this.taskDB = {};
|
||
|
|
},
|
||
|
|
|
||
|
|
// GET
|
||
|
|
get: function( _router, _args, _cb, _crypto ) {
|
||
|
|
|
||
|
|
// 判空
|
||
|
|
if( nx.dt.strEmpty( _router ) ) {
|
||
|
|
nx.dt.fnInvoke( _cb, nx.genError( ERR.NET.URLInvalid ) );
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 请求
|
||
|
|
this._request( "GET",
|
||
|
|
_router,
|
||
|
|
_args,
|
||
|
|
_cb,
|
||
|
|
_crypto );
|
||
|
|
|
||
|
|
},
|
||
|
|
|
||
|
|
// POST
|
||
|
|
post: function( _router, _args, _cb, _crypto ) {
|
||
|
|
|
||
|
|
// 判空
|
||
|
|
if( nx.dt.strEmpty( _router ) ) {
|
||
|
|
nx.dt.fnInvoke( _cb, nx.genError( ERR.NET.URLInvalid ) );
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 请求
|
||
|
|
this._request( "POST",
|
||
|
|
_router,
|
||
|
|
_args,
|
||
|
|
_cb,
|
||
|
|
_crypto );
|
||
|
|
|
||
|
|
},
|
||
|
|
|
||
|
|
// 上传
|
||
|
|
upload: function( _router, _args, _data, _cb ) {
|
||
|
|
|
||
|
|
// 判空
|
||
|
|
if( nx.dt.strEmpty( _router ) ) {
|
||
|
|
nx.dt.fnInvoke( _cb, nx.genError( ERR.NET.URLInvalid ) );
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 关键字
|
||
|
|
let key = this._genTaskKey( "POST", _router );
|
||
|
|
// if( this.taskDB[key] ) {
|
||
|
|
// nx.warn( "[WEB]重复请求:", key );
|
||
|
|
// nx.dt.fnInvoke( _cb, nx.genError( ERR.NET.RequestRepeat ) );
|
||
|
|
// return;
|
||
|
|
// }
|
||
|
|
|
||
|
|
// URL规范
|
||
|
|
let url = _router;
|
||
|
|
if( _router.indexOf( 'http' ) != 0 ) {
|
||
|
|
url = cc.path.join( this.url, _router );
|
||
|
|
}
|
||
|
|
|
||
|
|
// 创建新任务
|
||
|
|
let args = {
|
||
|
|
mgr : this,
|
||
|
|
key : key,
|
||
|
|
reqURL : url,
|
||
|
|
cb : _cb,
|
||
|
|
reqType : "POST",
|
||
|
|
reqArgs : _args,
|
||
|
|
crypto : false,
|
||
|
|
};
|
||
|
|
let task = nx.factory.create( "nx.web.task", args );
|
||
|
|
if( !task ) {
|
||
|
|
nx.dt.fnInvoke( _cb, nx.genError( ERR.NET.CreateTaskFailed ) );
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// this.taskDB[key] = task;
|
||
|
|
task.upload( _data );
|
||
|
|
|
||
|
|
},
|
||
|
|
|
||
|
|
// 请求
|
||
|
|
_request: function( _type, _router, _args, _cb, _crypto ) {
|
||
|
|
|
||
|
|
_cb = _cb || nx.dt.fnEmpty;
|
||
|
|
|
||
|
|
// 方式验证
|
||
|
|
if( _type != "GET" && _type != "POST" ) {
|
||
|
|
nx.dt.fnInvoke( _cb, nx.genError( ERR.NET.RequestTypeInvalid ) );
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 参数验证
|
||
|
|
if( nx.dt.strEmpty( _router ) ||
|
||
|
|
!nx.dt.objGood( _args ) ) {
|
||
|
|
nx.dt.fnInvoke( _cb, nx.genError( ERR.NET.RequestArgsInvalid ) );
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 关键字
|
||
|
|
let key = this._genTaskKey( _type, _router );
|
||
|
|
// if( this.taskDB[key] ) {
|
||
|
|
// nx.warn( "[WEB]重复请求:", key );
|
||
|
|
// nx.dt.fnInvoke( _cb, nx.genError( ERR.NET.RequestRepeat ) );
|
||
|
|
// return;
|
||
|
|
// }
|
||
|
|
|
||
|
|
// URL规范
|
||
|
|
let url = _router;
|
||
|
|
if( _router.indexOf( 'http' ) != 0 ) {
|
||
|
|
url = cc.path.join( this.url, _router );
|
||
|
|
}
|
||
|
|
|
||
|
|
// 创建新任务
|
||
|
|
let args = {
|
||
|
|
mgr : this,
|
||
|
|
key : key,
|
||
|
|
reqURL : url,
|
||
|
|
cb : _cb,
|
||
|
|
reqType : _type,
|
||
|
|
reqArgs : _args,
|
||
|
|
crypto : _crypto || false,
|
||
|
|
};
|
||
|
|
let task = nx.factory.create( "nx.web.task", args );
|
||
|
|
if( !task ) {
|
||
|
|
nx.dt.fnInvoke( _cb, nx.genError( ERR.NET.CreateTaskFailed ) );
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// this.taskDB[key] = task;
|
||
|
|
task.send();
|
||
|
|
|
||
|
|
},
|
||
|
|
|
||
|
|
// 任务执行回调
|
||
|
|
_onTaskCallback: function( _task ) {
|
||
|
|
|
||
|
|
if( !_task ) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 任务移除
|
||
|
|
// delete this.taskDB[_task.key];
|
||
|
|
|
||
|
|
// 监听通告
|
||
|
|
if( nx.dt.fnGood( _task.listener ) ) {
|
||
|
|
_task.listener( _task.ansCode, _task.ansData );
|
||
|
|
}
|
||
|
|
|
||
|
|
// 任务销毁
|
||
|
|
nx.factory.remove( _task );
|
||
|
|
|
||
|
|
},
|
||
|
|
|
||
|
|
// 生成任务关键字
|
||
|
|
_genTaskKey: function( _type, _router ) {
|
||
|
|
let key = cc.js.formatStr( "%s-%s", _type, _router );
|
||
|
|
return key.toLowerCase();
|
||
|
|
},
|
||
|
|
|
||
|
|
});
|
||
|
|
|
||
|
|
module.exports = NxWebManager;
|