そうすると、マルチタスクのイベントドリブンのような顔をしたActionScriptではあるけど、
正しくタイムシェアリングしてるわけでもなんでもないし、
サーバとの転送量が大きい場合(データが大きい画像とか)を考えると、
1つづつ実行できるようなシステムにしたいわけですね。
といったところで、画像一覧をゲットするようなことを考えて見る。
●httpのためのクラスを作る
【仕様】
→url、データのタイプ、パラメータ、callBack関数を複数指定して、
ひとつづつhttpリクエストをかける。
100コの画像を取ってくる場合でも、必ず、ひとつずつリクエストするものとする。
いろいろやって結果、スマートかどうかは別にして、、、
タイマを使うことになってしまった。
要するに、
実行中かどうかをフラグで判定して、判定中だったら、タイマをまた設定みたいなことを繰り返す。
ActionScriptにはSleepも無いことだし。
ちょっといい加減に書きなぐったSRCがこんな感じ。
なんとなくそれらしく動く。パラメータとかTEXT系じゃないときは、次に考えます。
function spHttp(){
own = this;
init();
}
// ------------------------------------------------------------------------------
public function init():void{
datAr = new Array;
}
// ------------------------------------------------------------------------------
public function setData(url:String, comtype:uint, okRes:Function, ngRes:Function, method:String="POST",useProxy:Boolean=false):void{
datAr.unshift(
{
"url": url,
"comtype": comtype,
"okFunc": okRes,
"ngFunc": ngRes,
"method": method,
"useProxy": useProxy
}
);
}
// ------------------------------------------------------------------------------
public function execute(){
setTimeout(executeSub, tInterval);
}
// ------------------------------------------------------------------------------
public function executeSub(){
if(datAr.length < 1){
// END
}else{
if(bRun){
setTimeout(executeSub, tInterval);
}else{
bRun = true;
var o:Object = datAr.pop();
httpSV = new HTTPService();
httpSV.addEventListener(ResultEvent.RESULT, okResult);
httpSV.addEventListener(FaultEvent.FAULT, ngResult);
httpSV.resultFormat = "text"; //
httpSV.url = o.url; //
httpSV.method = o.method; // httpSV.method = "POST";
httpSV.useProxy = o.useProxy; // httpSV.useProxy =false;
uVar = new URLVariables;
uVar.decode("Giant=Baba");
var at:AsyncToken = httpSV.send(uVar);
at.id = o.comtype;
okFunction = o.okFunc;
ngFunction = o.ngFunc;
}
}
}
// ------------------------------------------------------------------------------
public function okResult(event:ResultEvent):void{
httpSV.removeEventListener(ResultEvent.RESULT,okResult);
httpSV.removeEventListener(FaultEvent.FAULT,ngResult);
okFunction(event); // 本当の処理はこっちだよ
bRun = false;
execute();
}
// ------------------------------------------------------------------------------
public function ngResult(event:FaultEvent):void
{
httpSV.removeEventListener(ResultEvent.RESULT,okResult);
httpSV.removeEventListener(FaultEvent.FAULT,ngResult);
ngFunction(event); // 本当の処理はこっちだよ
bRun = false;
execute();
}
// ------------------------------------------------------------------------------
0 件のコメント:
コメントを投稿