标签:as3 http
每个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” 博客,转载请与作者联系!
标签:as3 http
原文地址:http://aonaufly.blog.51cto.com/3554853/1828786