2011-04-06 40 views
0

我加載XML文件,這個簡單的Flash CS5/ActionScript 3的程序:爲什麼URLStream.connected始終爲真?

import flash.net.*; 

var URL_REQUEST:URLRequest = new URLRequest('http://preferans.de/top-xml.php'); 
var URL_STREAM:URLStream = new URLStream(); 
var URL_VARS:URLVariables = new URLVariables(); 
var UPDATE_TIMER:Timer = new Timer(1000); 

stop(); 

UPDATE_TIMER.addEventListener(TimerEvent.TIMER, handleTimer); 
UPDATE_TIMER.start(); 

URL_REQUEST.method = URLRequestMethod.GET; 
URL_REQUEST.data = URL_VARS; 

URL_STREAM.addEventListener(IOErrorEvent.IO_ERROR, handleUserError); 
URL_STREAM.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handleUserError); 
URL_STREAM.addEventListener(Event.OPEN, handleUserOpen); 
URL_STREAM.addEventListener(ProgressEvent.PROGRESS, handleUserData); 
URL_STREAM.addEventListener(HTTPStatusEvent.HTTP_STATUS, handleUserStatus); 
URL_STREAM.addEventListener(Event.COMPLETE, handleUserComplete); 
URL_STREAM.load(URL_REQUEST); 

function handleUserOpen(event:Event):void { 
    trace('handleUserOpen: ' + event); 
} 

function handleUserData(event:Event):void { 
    trace('handleUserData: ' + event); 
} 

function handleUserStatus(event:HTTPStatusEvent):void { 
    trace('handleUserStatus: ' + event.status); 
} 

function handleUserError(event:Event):void { 
    trace('handleUserError: ' + event); 
} 

function handleUserComplete(event:Event):void { 
    trace('handleUserComplete: ' + event); 

    try { 
     var str:String = URL_STREAM.readUTFBytes(URL_STREAM.bytesAvailable); 
     var xml:XML = new XML(str); 
     trace(xml); 
    } catch(e:Error){ 
     trace('Invalid data: ' + e); 
     return; 
    } 
} 

function handleTimer(event:TimerEvent):void { 
    var now:int = getTimer(); 

    trace(UPDATE_TIMER.currentCount + ' ' + now + ' ' + URL_STREAM.connected); 
} 

它工作正常,我可以看到XML內容:

handleUserOpen: [Event type="open" bubbles=false cancelable=false eventPhase=2] 
handleUserData: [ProgressEvent type="progress" bubbles=false cancelable=false eventPhase=2 bytesLoaded=2390 bytesTotal=2390] 
handleUserStatus: 200 
handleUserComplete: [Event type="complete" bubbles=false cancelable=false eventPhase=2] 
<pref> 
    [ .... XML content .....] 
</pref> 
1 1054 true 
2 2054 true 
3 3054 true 
4 4054 true 
5 5054 true 
..... 
90 90054 true 
91 91054 true 

,但我不明白,爲什麼是URLStream.connected總是true

我甚至在我的web服務器上重新啓動Apache,但它不會改變任何東西。

我在問這個問題,因爲我計劃在我的程序中實現Comet-like(又名HTTP-push)調用,並且需要知道,如果URLStream仍然工作/忙碌,或者它已完成/中斷,並且可以重新用於新的加載()調用(並且我不想爲此引入變通辦法狀態變量)。

謝謝! 亞歷克斯

回答

1

我認爲與URLStream只要它訪問該文件的連接保持建立,即使它還沒有開始下載任何東西或已完成下載的東西。所以我認爲在完成讀取值之後,您必須在.COMPLETE函數中手動.close()。