为什么 URLStream.connected 总是 true?
我使用这个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为对于 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.