开发者

Why is URLStream.connected always true?

I load an XML file with this simple Flash CS5 / ActionScript 3 program:

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, handleUserComple开发者_如何学编程te);
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);
}

it works fine and I can see the XML content:

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

but I don't understand, why is URLStream.connected always true.

I even restart Apache at my web server, but it doesn't change anything.

I'm asking this question, because I plan implementing Comet-like (aka HTTP-push) calls in my program and need to know, if URLStream is still working/busy or if it is completed/interrupted and can be reused for a new load() call (and I don't want to introduce a workaround state variable for that).

Thank you! Alex


I think with URLStream as long it accesses the file its connection stays established, even when it hasn't started downloading anything or has finished downloading something. So I think you have to .close() it manually in the .COMPLETE function after your done reading the values.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜