HTTP for AS3

Source: Internet
Author: User

Each HTTP request gives Config.tryhttpreq a chance. and distributed request 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 Management class   *  @author  kayer  * */ public final class httpmanager  {  /**   * http Request Queue    * */  private var  _teamHttpVec : Vector.<HttpModelREQ> = new Vector.<HttpModelREQ> ();   private var _curHttpModelREQ : HttpModelREQ = null;   Private static var ins : httpmanager;  private var _urlloader : urlloader;  /** request busy? */  private var _isrequestbusy  : boolean = false;  /** number of requests */  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  is designed for simple interest class"  );    else   {    this._ Urlloader = new urlloader ();    this._urlloader.dataformat =  Urlloaderdataformat.text;    this._urlloader.addeventlistener (Event.COMPLETE,  Loadercompletehandler);    &Nbsp;this._urlloader.addeventlistener (Httpstatusevent.http_status, httpstatushandler);     this._urlloader.addeventlistener (Securityerrorevent.security_error, securityerrorhandler);     this._urlloader.addeventlistener (Ioerrorevent.io_error, ioerrorhandler);    }  }  /**   *  Send HTTP requests    *  @param  $ Httpmodelreq : http Model    */  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} requests [{1}] succeeded  ,  returned result: {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&NBSP;:"  + e )  ;//   if ( this._ Curhttpmodelreq.errorcallback != null )//   {//    this._curhttpmodelreq.errorcallback (e)//    }//   this.nextbystep ();  }  private function   Securityerrorhandler ( e : SecurityErrorEvent )  : void  {    trace ( gameutil.parsestr ("  {0} times request [{1}] failed  , {2}: Error message {3}"  , [ this.$ trycount , this._curhttpmodelreq.url , "Ioerrorevent"  , e ] )  )  ;    if ( this. $tryCount  < Config.tryHttpREQ )    {     this. $tryCount  += 1;    //  retry http ,  request      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} Second request [{1}] failed  , {2}: Error message {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 ();   }  }  /**   *  continue to process     */  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 class

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 Request Model    *  @author  Kayer  * */ public final class HttpModelREQ  {  private var _url : string;  private var _succcallback  : function;  private var _isgetmethod : boolean;  private  var _errorcallback : function;  /** parameter */  private var _ params : dictionary;  /**   *  @param   $url  :  request address     *  @param   $succCallBack  :  Successful callback function    *  @param   $params  :  Request Parameter    * @param  $isGetMethod  :  Request mode  GET/POST   *  @param   $errorCallBack  :  request failed callback    */  public function httpmodelreq (  $url  :  String ,  $succCallBack  : Function ,  $params  : dictionary = &NBSP;NULL&NBSP, $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;  }  /**   *  Destruction     */  public function destory ()  : void  {   this._ Succcallback = null;   if ( this._errorCallBack != null )      this._errorcallback = null;  } }}

Application Method:

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 ("Cal    L Back "+ $str);    }, $params, false, NULL); HttpManager.instance.sendHttpREQ ($httpModel);

This article from "Better_power_wisdom" blog, reproduced please contact the author!

HTTP for AS3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.