1. 云栖社区>
  2. PHP教程>
  3. 正文

AS3的http

作者:用户 来源:互联网 时间:2017-12-01 17:22:55

httpas3

AS3的http - 摘要: 本文讲的是AS3的http,每个Http请求给予Config.tryHttpREQ次机会.并且分布式请求Httppackage com.game.system.net.http.kayer{ import com.game.common.Config; import

每个Http请求给予Config.tryHttpREQ次机会.并且分布式请求Http
package com.game.system.net.http.kayer{ import com.game.common.Config; import com.game.common.GameUtil;  import flash.events.Event; import flash.events.HTTPStatusEvent; import flash.events.IOErrorEvent; import flash.events.SecurityErrorEvent; import flash.net.URLLoader; import flash.net.URLLoaderDataFormat; import flash.utils.Dictionary; /**  * Http管理类  * @author Kayer  * */ public final class HttpManager {  /*** Http请求队列* */  private var _teamHttpVec : Vector.<HttpModelREQ> = new Vector.<HttpModelREQ>();  private var _curHttpModelREQ : HttpModelREQ = null;  private static var ins : HttpManager;  private var _urlLoader : URLLoader;  /**请求繁忙?*/  private var _isRequestBusy : Boolean = false;  /**请求次数*/  private var $tryCount : uint = 1;  public static function get instance() : HttpManager  {if( !ins ) ins = new HttpManager();return ins;  }  public function HttpManager()  {if( ins ) throw Error( "HttpManager 被设计为单利类" );else{ this._urlLoader = new URLLoader(); this._urlLoader.dataFormat = URLLoaderDataFormat.TEXT; this._urlLoader.addEventListener(Event.COMPLETE, loaderCompleteHandler); this._urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler); this._urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); this._urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);}  }  /*** 发送Http请求* @param $httpModelReq : http模型*/  public function sendHttpREQ( $httpModelReq : HttpModelREQ ) : void  {if( !this._isRequestBusy ){ this._isRequestBusy = true; this._curHttpModelREQ = $httpModelReq; this._curHttpModelREQ.sendREQ( this._urlLoader );}else{ this._teamHttpVec.push( $httpModelReq );}  }  private function loaderCompleteHandler( e : Event ) : void  {if( this._curHttpModelREQ != null ){ trace( GameUtil.parseStr(" 第{0}次请求[{1}]成功 , 返回结果:{2}" , [ this.$tryCount , this._curHttpModelREQ.url ,e.target.data ] ) ) ; this._curHttpModelREQ.succCallBack( e.target.data );}this.$tryCount = 1;this.nextByStep();  }  private function httpStatusHandler( e :  HTTPStatusEvent ) : void  {//trace( "HTTPStatusEvent :" + e ) ;//if( this._curHttpModelREQ.errorCallBack != null )//{// this._curHttpModelREQ.errorCallBack(e)//}//this.nextByStep();  }  private function  securityErrorHandler( e : SecurityErrorEvent ) : void  {trace( GameUtil.parseStr(" 第{0}次请求[{1}]失败 , {2}:错误信息{3}" , [ this.$tryCount , this._curHttpModelREQ.url ,"IOErrorEvent" , e ] ) ) ;if( this.$tryCount < Config.tryHttpREQ ){ this.$tryCount += 1; // 重试Http , 请求 this._curHttpModelREQ.sendREQ( this._urlLoader );}else{ this.$tryCount = 1; if( this._curHttpModelREQ.errorCallBack != null ) {  this._curHttpModelREQ.errorCallBack(e) } this.nextByStep();}  }  private function ioErrorHandler( e : IOErrorEvent ) : void  {trace( GameUtil.parseStr(" 第{0}次请求[{1}]失败 , {2}:错误信息{3}" , [ this.$tryCount , this._curHttpModelREQ.url ,"IOErrorEvent" , e ] ) ) ;if( this.$tryCount < Config.tryHttpREQ ){ this.$tryCount += 1; }else{ this.$tryCount = 1; if( this._curHttpModelREQ.errorCallBack != null ) {  this._curHttpModelREQ.errorCallBack(e) } this.nextByStep();}  }  /*** 继续往下处理*/  private function nextByStep() : void  {if( this._curHttpModelREQ != null ) this._curHttpModelREQ.destory();if( this._teamHttpVec.length > 0 ){ this._curHttpModelREQ = this._teamHttpVec.pop(); this._curHttpModelREQ.sendREQ( this._urlLoader );}else this._isRequestBusy = false;  } }}


Item类
package com.game.system.net.http.kayer{ import flash.net.URLLoader; import flash.net.URLRequest; import flash.net.URLRequestMethod; import flash.net.URLVariables; import flash.utils.Dictionary; /**  * httpModel : http请求模型  * @author Kayer  * */ public final class HttpModelREQ {  private var _url : String;  private var _succCallBack : Function;  private var _isGetMethod : Boolean;  private var _errorCallBack : Function;  /**参数*/  private var _params : Dictionary;  /*** @param $url : 请求地址* @param $succCallBack : 成功回调函数* @param $params : 请求参数* @param $isGetMethod : 请求模式 GET/POST* @param $errorCallBack : 请求失败回调*/  public function HttpModelREQ( $url : String , $succCallBack : Function , $params : Dictionary = null ,$isGetMethod :Boolean = true  , $errorCallBack : Function = null )  {this._url = $url;this._succCallBack = $succCallBack;this._params = $params;this._isGetMethod = $isGetMethod;this._errorCallBack = $errorCallBack;  }  public function sendREQ( $urlLoader : URLLoader ) : void  {var $urlRequest : URLRequest = new URLRequest( this._url );$urlRequest.method = this._isGetMethod ? URLRequestMethod.GET : URLRequestMethod.POST;if( this._params != null ){ var $variables : URLVariables = new URLVariables(); for( var $key : * in this._params ) {  $variables[$key] = this._params[$key]; } $urlRequest.data = $variables;}$urlLoader.load( $urlRequest );  }  public function get succCallBack() : Function  {return this._succCallBack;  }  public function get errorCallBack() : Function  {return this._errorCallBack;  }  public function get url() : String  {return this._url;  }  /*** 销毁*/  public function destory() : void  {this._succCallBack = null;if( this._errorCallBack != null ) this._errorCallBack = null;  } }}


应用方法:
var $params : Dictionary = new Dictionary(); $params["kindid"] = 1; $params["nickname"] = "XXXX"; $params["uid"] = "884"; $params["code"] = "f4e2637a8eee9820b";var $httpModel : HttpModelREQ = new HttpModelREQ( "http:www.baidu.com" , function( $str : String  ) : void{  trace("Call back " + $str); } , $params , false , null ); HttpManager.instance.sendHttpREQ( $httpModel );

本文出自 “Better_Power_Wisdom” 博客,转载请与作者联系。

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索http as3 ,以便于您获取更多的相关知识。

稳定可靠、可弹性伸缩的在线数据库服务,全球最受欢迎的开源数据库之一

6款热门基础云产品6个月免费体验;2款产品1年体验;1款产品2年体验

弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率

开发者常用软件,超百款实用软件一站式提供