为什么 URLStream.connected 总是 true?

发布于 2024-10-30 15:42:17 字数 2582 浏览 5 评论 0原文

我使用这个简单的 Flash CS5 / ActionScript 3 程序加载 XML 文件:

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

我什至在我的网络服务器上重新启动 Apache,但它没有改变任何东西。

我问这个问题是因为我计划在我的程序中实现类 Comet(又名 HTTP 推送)调用,并且需要知道 URLStream 是否仍在工作/繁忙,或者它是否已完成/中断并且可以重用于新的load() 调用(我不想为此引入解决方法状态变量)。

谢谢你! 亚历克斯

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, 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);
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难以启齿的温柔 2024-11-06 15:42:17

我认为对于 URLStream,只要它访问文件,它的连接就会保持建立,即使它尚未开始下载任何内容或已完成下载某些内容。所以我认为在读取完值后,您必须在 .COMPLETE 函数中手动 .close() 它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文