程序员

LayaBox Http请求封装管理

2018-08-03  本文已影响15人  3c9a4f007e1b
public class Http
    {
        private static var _requests:Array = [];
        
        private static function getRequest():HttpRequest
        {
            return _requests.length? _requests.shift() : new HttpRequest();
        }
        
        private static function onRequestError(e:*):void
        {
            trace(e);
        }
        
        private static function onRequestComplete(hr:HttpRequest, call:Handler):void
        {
            trace(hr.data);
            call.runWith(hr.data);
            _requests.push(hr);
        }
        
        private static function send(path:String,call:Handler,type:String):void
        {
            var hr:HttpRequest = getRequest();
            hr.once(Event.COMPLETE, Http, onRequestComplete,[hr,call]);
            hr.once(Event.ERROR, Http, onRequestError);
            hr.send(path, null, type, 'text');
        }
        
        public static function get(path:String,call:Handler):void
        {
            send(path, call, "get");
        }
        
        // 未完待续
        
    }

如果疑问,可加QQ:29727880

上一篇下一篇

猜你喜欢

热点阅读