当前位置:   article > 正文

COCOS CREATOR(TS) 之HTTP通信

cocoscreator http请求管理类

一 : XMLHttpRequest的封装

  1. export class HttpCell{
  2. private _xhr : XMLHttpRequest = null;
  3. private _server_url : string = null;
  4. private _callback : ( $isSucc : boolean , _http : HttpCell ,$data : any ) => void = null;
  5. private _timeout : number = null;
  6. private _formData : FormData = null;
  7. private _method : string = null;
  8. public constructor(){
  9. this._xhr = new XMLHttpRequest();
  10. this.listener2Handler( true );
  11. }
  12. public abortListener() : void{
  13. this.listener2Handler( false );
  14. this._server_url = null;
  15. this._callback = null;
  16. }
  17. public reset : Function = () : void => {
  18. this.listener2Handler( true );
  19. }
  20. private onComplete( $isSucc : boolean , $data : any , $target : HttpCell ) : void {
  21. if( this === $target ){
  22. this._callback( $isSucc , this , $data );
  23. }
  24. }
  25. private listener2Handler : Function = ( $isAdd : boolean ) : void => {
  26. if( $isAdd ){
  27. ListenerManager.getInstance().add( ListenerType.NetHttpComplete , this , this.onComplete.bind(this) );
  28. this._xhr.onload = ( ev: Event) : any => {
  29. if( this._xhr.status == 200 || this._xhr.status == 304 ){
  30. let $res : any = 'response' in this._xhr ? this._xhr.response : this._xhr.responseText;
  31. cc.warn(`[Http] error ${$res}`);
  32. }
  33. }
  34. if( this._timeout ){
  35. this._xhr.ontimeout = ( ev: ProgressEvent ) : any => {
  36. cc.warn(`[Http] error timeout!`);
  37. }
  38. }
  39. this._xhr.onerror = (ev: ErrorEvent) : any => {
  40. cc.warn( `[Http] error ${ev.message}` );
  41. }
  42. this._xhr.onreadystatechange = () : void => {
  43. if (this._xhr.readyState == XMLHttpRequest.DONE && ( this._xhr.status >= 200 && this._xhr.status < 400) ) {
  44. if( this._xhr.status == 200 ){
  45. ListenerManager.getInstance().trigger( ListenerType.NetHttpComplete , true , this._xhr.response , this);
  46. }else{
  47. ListenerManager.getInstance().trigger( ListenerType.NetHttpComplete , false , this._xhr.status , this );
  48. }
  49. this._xhr.abort();
  50. }
  51. }
  52. }else{
  53. if( this._xhr.onload ) this._xhr.onload = null;
  54. if( this._xhr.ontimeout ) this._xhr.ontimeout = null;
  55. if( this._xhr.onerror ) this._xhr.onerror = null;
  56. if( this._xhr.onreadystatechange ) this._xhr.onreadystatechange = null;
  57. ListenerManager.getInstance().remove( ListenerType.NetHttpComplete , this , this.onComplete.bind(this) );
  58. }
  59. }
  60. public send : Function = (
  61. $server_url : string ,
  62. $data : object ,
  63. $callback : ( $isSucc : boolean , _http : HttpCell ,$data : any ) => void,
  64. $dataFormat : TYPE_HTTP4DATAFORMAT = TYPE_HTTP4DATAFORMAT.___TEXT___ ,
  65. $isGet : boolean = true,
  66. $timeout : number = null
  67. ) : void => {
  68. this._server_url = $server_url;
  69. this._callback = $callback;
  70. this._timeout = $timeout;
  71. this._method = $isGet ? "GET" : "POST";
  72. if( $timeout ) this._xhr.timeout = $timeout;
  73. switch( $dataFormat ){
  74. case TYPE_HTTP4DATAFORMAT.___TEXT___: this._xhr.responseType = "text";break;
  75. case TYPE_HTTP4DATAFORMAT.___BINARY___ : this._xhr.responseType = "arraybuffer";break;
  76. case TYPE_HTTP4DATAFORMAT.___JSON___ : this._xhr.responseType = "json";break;
  77. }
  78. if( $data ){
  79. this._formData = new FormData();
  80. for( let $key of Object.keys($data) ){
  81. this._formData.append( $key + `` , $data[$key] + `` );
  82. }
  83. }else{
  84. this._formData = null;
  85. }
  86. this.start();
  87. }
  88. private start : Function = () : void => {
  89. this._xhr.open( this._method , this._server_url , true );
  90. this._xhr.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
  91. this._xhr.send( this._formData );
  92. }
  93. public tryAgain : Function = () : void => {
  94. this.start();
  95. }
  96. public destory() : void{
  97. this.abortListener();
  98. this._xhr = null;
  99. }
  100. }

PS :
Ⅰ,onreadystatechange : 当HTTP请求状态改变时触发
①,status 状态
②,response 得到后端返回的数据

Ⅱ,tryAgain : 当Http请求出现异常时 , 可以请求tryAgain重新请求一次

Ⅲ,send方法
①, 参数 $data = { name="123" , pwd="123456" }

二 : 数据类型

  1. export enum TYPE_HTTP4DATAFORMAT{
  2. ___TEXT___ = 1,
  3. ___BINARY___ = 2,
  4. ___JSON___ = 3
  5. }

三 : 封装Http管理类

  1. export class HttpNetManager{
  2. private static _instance : HttpNetManager = null;
  3. public static get Instance() : HttpNetManager{
  4. if( !HttpNetManager._instance )
  5. HttpNetManager._instance = new HttpNetManager();
  6. return HttpNetManager._instance;
  7. }
  8. private _list_cell : PoolObjects<IHttp_Data> = null;
  9. private _cur_loading : Map<HttpCell,IHttp_Data> = null;
  10. private readonly _try_count : number = 3;
  11. private readonly _try_wait_during : number = 350;
  12. private constructor(){
  13. this._list_cell = new PoolObjects(3);
  14. this._cur_loading = new Map();
  15. }
  16. public send : Function = (
  17. $server_url : string ,
  18. $data : object ,
  19. $callback : ( $isSucc : boolean ,$data : any ) => void,
  20. $dataFormat : TYPE_HTTP4DATAFORMAT = TYPE_HTTP4DATAFORMAT.___TEXT___ ,
  21. $isGet : boolean = true,
  22. $isTry : boolean = true,
  23. $timeout : number = null
  24. ) : void => {
  25. let $modle : IHttp_Data = this._list_cell.Free;
  26. let $cell : HttpCell = null;
  27. if( !$modle ){
  28. $cell = new HttpCell();
  29. $modle = {
  30. _http : $cell ,
  31. _isTry : $isTry ,
  32. _callback : $callback,
  33. _cur_index : 0
  34. };
  35. this._list_cell.addNew( $modle );
  36. }else{
  37. $cell = $modle._http;
  38. $modle._isTry = $isTry;
  39. $modle._callback = $callback;
  40. $modle._cur_index = 0;
  41. $cell.reset();
  42. }
  43. this._cur_loading.set( $cell , $modle );
  44. $cell.send(
  45. $server_url,
  46. $data,
  47. this.onHttpCallback.bind(this),
  48. $dataFormat,
  49. $isGet,
  50. $timeout
  51. );
  52. }
  53. private onHttpCallback : Function = ( $isSucc : boolean , _http : HttpCell ,$data : any ) : void => {
  54. let $model : IHttp_Data = this._cur_loading.get( _http );
  55. const $complete : Function = () : void =>{
  56. $model._callback( $isSucc , $data );
  57. this._cur_loading.delete( _http );
  58. _http.abortListener();
  59. if( !this._list_cell.reInsert( $model ) ){
  60. _http.destory();
  61. }
  62. }
  63. if( !$isSucc ){
  64. if( $model._isTry && $model._cur_index < this._try_count ){
  65. $model._cur_index ++;
  66. if( this._try_wait_during > 0 ){
  67. setTimeout( () : void => {
  68. _http.tryAgain();
  69. } , this._try_wait_during );
  70. }else{
  71. _http.tryAgain();
  72. }
  73. }else{
  74. $complete();
  75. }
  76. }else{
  77. $complete();
  78. }
  79. }
  80. }

PS:
Ⅰ,_try_count参数 : 如果Http请求失败 , 会再次请求几次
Ⅱ,_try_wait_during: 等待多少毫秒后再次进行HTTP请求
Ⅲ,通过send发送Http请求 , 其参数$callback用来返回数据

转载于:https://blog.51cto.com/aonaufly/2346900

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/152686?site
推荐阅读
相关标签
  

闽ICP备14008679号