【问题标题】:Why is URLStream.connected always true?为什么 URLStream.connected 总是正确的?
【发布时间】:2011-04-06 18:24:17
【问题描述】:

我用这个简单的 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-push)的调用,并且需要知道 URLStream 是否仍在工作/忙碌,或者它是否已完成/中断并且可以重用对于新的 load() 调用(我不想为此引入解决方法状态变量)。

谢谢! 亚历克斯

【问题讨论】:

    标签: actionscript-3 http actionscript comet


    【解决方案1】:

    我认为使用 URLStream 只要它访问文件,它的连接就会保持建立,即使它还没有开始下载任何东西或已经完成下载。因此,我认为您必须在完成读取值后在 .COMPLETE 函数中手动 .close() 。

    【讨论】:

      猜你喜欢
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 2011-04-19
      • 2013-02-22
      • 2015-07-25
      • 1970-01-01
      相关资源
      最近更新 更多