本文介绍了的URLLoader卡时,轮询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:不知何故该工程运行Flash的浏览器的时候,但如果你从IDE中运行不起作用。你可能会想尝试在浏览器中运行,如果你有同样的问题。

我正在反复读取使用Flash&放我的服务器一个文本文件中的聊天应用程序; ActionScript 3.0中。我打开使用URLLoader文件,并能正常工作在第一。然而经过约10来电,的URLLoader卡。

I am making a chat application that repeatedly reads a text file from my server using Flash & Actionscript 3.0. I am opening the file with URLLoader, and it works fine at first. However after around 10 calls, the URLLoader gets stuck.

它不给一个完成事件,不给安全错误,不给一个状态事件并不会抛出异常。它只是永远不会触发任何事件都没有。我甚至增加了一个随机值到URL,以确保它是不是有些缓存问题。我能察觉,当它被卡住,当然,但似乎没有任何办法来粘住了。即使我调用close()上的URLLoader然后将其设置为null,创造一个又一个,它不会继续投票。

It does not give a completion event, does not give a security error, does not give a status event and does not throw an exception. It simply never fires any event at all. I even added a random value to the URL to make sure it is not some caching issue. I can detect when it gets stuck of course, but there doesn't seem to be any way to unstuck it. Even if I call close() on the URLLoader and then set it to null and create another one, it won't resume polling.

下面是函数轮询服务器和被调用一次,每两秒钟。

Below is the function that polls the server and gets called once every two seconds.

private function check_server() {
 var url:String = "http://coworkthailand.com/say/snd/index.php?"+Math.random();
 if (loader != null) {
  trace("was already checking "+loader.bytesLoaded+" / "+loader.bytesTotal);
  return;
 }
 loader =  new URLLoader();
 loader.dataFormat = URLLoaderDataFormat.TEXT;
 loader.addEventListener(Event.COMPLETE, completeHandler);
 loader.addEventListener(flash.events.IOErrorEvent.IO_ERROR, 
  function(e:Event) { loader = null; trace("fail"); })
 loader.addEventListener(flash.events.SecurityErrorEvent.SECURITY_ERROR, 
  function(e:Event) { loader = null; trace("security error"); })
 loader.addEventListener(flash.events.HTTPStatusEvent.HTTP_STATUS,
  function(e:flash.events.HTTPStatusEvent) { trace("status "+e.status); });
 try {
  loader.load(new URLRequest(url));
 } catch (error:Error) {
  trace("Unable to load requested document.");
 }
}

这是不是一个重要的项目,但任何想法将是非常美联社preciated!

This is not an important project, but any ideas would be much appreciated!

推荐答案

对于这种情况,最好的做法要求使用的URLLoader的一个实例;机会是你取得了一些相互竞争的呼叫阻塞。同样,您可以尝试使用一个简单的标志来检测你的装载机忙着做另外一个前现有的呼叫是;这里是一个使用单一的装载机发出请求每五秒钟,同时显示单装载机和isOpen会的工作示例检查:

For this scenario, best practice dictates using a single instance of URLLoader; chances are you're getting blocked somewhere by competing calls. As well, you might try using a simple flag to detect whether your loader's busy on an existing call before making another one; here's a working example using a single loader making requests every five seconds, showing both the single loader and the "isOpen" check:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="app_init()">    
    <mx:Script>
    	<![CDATA[

    		private var timer:Timer;
    		private var loader:URLLoader;
    		private var isOpen:Boolean;

    		private function app_init():void
    		{
    			timer = new Timer(5000)
    			timer.addEventListener(TimerEvent.TIMER, timer_tick, false, 0, true)

    			loader = new URLLoader();
    			loader.addEventListener(Event.OPEN, loader_open);
    			loader.addEventListener(Event.COMPLETE, loader_complete);

    			// Start the timer
    			timer.start();
    		}

    		private function timer_tick(event:TimerEvent):void
    		{
    			// Check if the loader's busy before calling load, and/or close()
    			if (!isOpen)
    			{
    				// loader.close();
    				loader.load(new URLRequest("http://mydomain.com/myfile.txt"));
    			}	
    		}

    		private function loader_open(event:Event):void
    		{
    			// Mark as open
    			isOpen = true;
    		}

    		private function loader_complete(event:Event):void
    		{
    			// Do work

    			// Mark as closed
    			isOpen = false;
    		}

    	]]>
    </mx:Script>	
</mx:Application>

希望它能帮助!祝你好运。

Hope it helps! Good luck.

这篇关于的URLLoader卡时,轮询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 08:05