如何在使用UrlLoader时管理临时网络问题?

时间:2012-08-30 18:17:04

标签: actionscript-3 flash urlloader

我创建了一个RSS Ticker,可以在办公室的一堆显示器上使用。这个RSS Ticker将使用Twiter Feed阅读器,天气小部件和时钟在同一个.fla中运行。

由于有大量的这些显示器,我不希望每个人都上网搜索rss,twitter和天气数据。所有这些数据都包含在从Internet下载的许多简单XML文件中。

我已经创建了一个在我们的服务器上运行的Windows服务。此服务可以完成所有工作,包括查看更新和将新数据文件下载到网络上的共享。然后我让所有显示器上的闪存仪表板从网络副本中获取信息。这样,数据就会被下载一次,其他所有内容都只能访问这个本地缓存。

一切正常,但随机的RSS代码将停止工作。我没有办法远程访问这些显示器来检查他们的错误日志,但是当我拔下以太网电缆时,我在本地测试环境中遇到完全相同的问题。仪表板尝试访问网络RSS副本,失败,然后在计时器的每个滴答声中继续失败,即使在重新建立连接之后也是如此。

我希望能够优雅地等待断开连接,然后在显示器重新连接后重新启动RSS自动收报机。我不知道如何做到这一点。以下是我的RSS相关代码:

// used to load a local xml file (stored in the same folder as the dashboard) that tells 
// the dashboards which RSS feeds can be found on the network, and where they are located. 
// It also contains the limits for how long each feed should be shown (both a time limit, 
// and a maximimum number of feed items)

var rssListXMLLoader:URLLoader = new URLLoader();
rssListXMLLoader.addEventListener(Event.COMPLETE, rssListXMLLoaded); 
rssListXMLLoader.addEventListener(IOErrorEvent.IO_ERROR, ioRSSListXMLLoaderErrorHandler);


// used to load the individual RRS feeds 

var rssFeedXMLLoader:URLLoader = new URLLoader();
rssFeedXMLLoader.addEventListener(Event.COMPLETE, rssFeedXMLLoaded); 
rssFeedXMLLoader.addEventListener(IOErrorEvent.IO_ERROR, ioRSSFeedXMLLoaderErrorHandler);


// rssListXMLData:XML and rssFeedXMLData:XML are used to store the XML data that is returned
// by the above loaders.

var rssListXMLData:XML = new XML(); 
var rssFeedXMLData:XML = new XML();

var currentRSSIndex:int = 2^10;
var currentFeedIndex:int = 2^10;
var currentLimit_Feeds:int = 2^10;
var currentLimit_Minutes:int = 2^10;
var limitStartTime:Number = new Date().time;
var gettingNewFeed = false;

// the blow Timer is used to keep the ticker moving. Each tick, it checks to see of the
// current RSS on sceen is has any text left. If it does, it removes the first letter
// causing the string to appear as if it is scrolling from right to left. If it has no
// more text, it gets the next feed.

var rssTickerTMR:Timer = new Timer(100, 0);
rssTickerTMR.addEventListener(TimerEvent.TIMER, updateRSSTicker);
rssTickerTMR.start();

updateRSSTicker();

function updateRSSTicker(e:Event = null):void
{
    if(ins_rssContent.text.length == 0)
    {
        if(!gettingNewFeed)
        {
            gettingNewFeed = true;
            getNewFeed();
        }
    }
    else
    {
        ins_rssContent.text = ins_rssContent.text.substring(1);
    }
}


// the below function checks to see if the RSS List is loaded. If it isn't it loads it,
// if it is already loaded, it calls the rssListXMLLoaded. This way, rssListXMLLoaded is
// never called unless the RSS List is already loaded.

function getNewFeed():void
{
    if (rssListXMLData == "")
    {
        trace("Loading RSS List XML");
        rssListXMLLoader.load(new URLRequest("rssFeeds.xml"));
    }
    else
    {
        rssListXMLLoaded();
    }
}


// the below function manages which feed needs to be used from the list.

function rssListXMLLoaded(e:Event = null):void
{
    if (e != null)
    {
        rssListXMLData = new XML(e.target.data);
    }

    currentFeedIndex++;

    if (
            currentFeedIndex >= rssFeedXMLData.channel["item"].length() || 
            currentFeedIndex >= currentLimit_Feeds || 
            new Date().time - limitStartTime >= (currentLimit_Minutes * 1000 * 60)
    )
    {
        limitStartTime = new Date().time;
        currentFeedIndex = 0;
        currentRSSIndex++;      
        currentRSSIndex = (currentRSSIndex >= rssListXML.children().length()) ? 0 : currentRSSIndex;        
        currentLimit_Feeds = Number(rssListXMLData.RSSFeed[currentRSSIndex].@limitFeeds)
        currentLimit_Minutes = Number(rssListXMLData.RSSFeed[currentRSSIndex].@limitMinutes)
    }
    try
    {
        rssFeedXMLLoader.load(new URLRequest(rssListXMLData.RSSFeed[currentRSSIndex])); 
    }
    catch(e:Error)
    {
        trace("whoops, try again");
    }
}


// the below function sets the text on the dashboard, and sets gettingNewfeed to false. This allows the
// rssTickerTMR:Timer to start removing the first letter of the newly added feed, causing the text to "scroll"

function rssFeedXMLLoaded(e:Event):void
{
    rssFeedXMLData = new XML(e.target.data);

    if (rssFeedXMLData.channel["item"].length() > 0)
    {
        var dateString:String = rssFeedXMLData.channel["item"][currentFeedIndex].pubDate;
        var date:Date = new Date(Date.parse(dateString.replace(" Z","")));

        if (date.toString() != "Invalid Date")
        {
            dateString = " @ " + FormatDate(date) + " " + FormatTime(date);
        }
        else
        {
            dateString = " @ " + dateString;
        }

        ins_rssTitle.text = rssFeedXMLData.channel.title + dateString;

        ins_rssContent.text = "                                                                        " 
                            + cleanDescription(rssFeedXMLData.channel["item"][currentFeedIndex].title) + "   - "
                            + cleanDescription(rssFeedXMLData.channel["item"][currentFeedIndex].description);

        gettingNewFeed = false;
    }
    else
    {
        ins_rssContent.text = "";
        gettingNewFeed = false;
    }
}

// removes the things from the RSS content that I don't want.

function cleanDescription(p_string:String):String 
{
    if (p_string == null) 
    { 
        return ''; 
    }

    var str:String = p_string.replace(/<\/?[^>]+>/igm, ''); //remove html tags.
    str = str.split("\r").join(". "); //replace line breaks with '. '
    str = str.replace("\t"," "); //replace tabs with a single space.
    str = str.replace(/(?:\.\s){2,}/g,". "); //replace multiple '. ' with a single one.
    str = str.replace(/((https?|ftp|gopher|telnet|file|notes|ms-help):
                          ((\/\/)|(\\\\))+[\w\d:#@%\/;$()~_?\+-=\\\.&]*)/igm, ' '); //replaces URL text with a single space
    str = str.replace(/#[\w]{3,}/igm,' ') //replaces hash tags with a single space
    str = str.replace(/\s{2,}/igm," "); //replace multiple spaces with a single space

    return str;
}

function ioRSSListXMLLoaderErrorHandler(e:Event):void
{
    trace("ioRSSListXMLLoaderErrorHandler:");
    trace(e.toString());

}


// the idea below is that it sets the text to "" and sets gettingNewFeed to false, which should get the
// rssTickerTMR:Timer to try to get a band new feed. Theoretically, it should try and fail over and again
// until the network is once again accessible. This doesn't work at all.

function ioRSSFeedXMLLoaderErrorHandler(e:Event):void
{
    trace("ioRSSFeedXMLLoaderErrorHandler:");
    trace(e.toString());

    ins_rssContent.text = "";
    gettingNewFeed = false;
}

有谁知道更好的方法来解决这个问题?如果您能提供任何帮助,我将不胜感激。谢谢!

EDIT !!!

根据Will的建议,我尝试创建一个基于计时器的网络监控子系统。在我的RSS Ticker的IO_ERROR处理程序中,我停止RSS计时器,并启动网络监视计时器。每隔10秒,它会尝试从服务器下载一个文件。它继续这样做直到它工作,此时它关闭自己的计时器,重新启动RSS计时器。

理论上应该有效。不仅如此,如果我将其更改为下载网站而不是网络文件,它确实有效。使用网络文件时,一旦失败,即使重新建立连接,也会一直失败。这是我的代码:

...

function ioRSSFeedXMLLoaderErrorHandler(e:Event):void
{
    rssTickerTMR.stop();
    networkMonitorTMR.start();

    trace("ioRSSFeedXMLLoaderErrorHandler:");
    trace(e.toString());

    ins_rssTitle.text = "Waiting for connection...";
    ins_rssContent.text = "";


}

// netowrk monitor
var networkMonitorXMLLoader:URLLoader = new URLLoader();
networkMonitorXMLLoader.addEventListener(Event.COMPLETE, networkMonitorXMLLLoaded); 
networkMonitorXMLLoader.addEventListener(IOErrorEvent.IO_ERROR, networkMonitorXMLLoaderErrorHandler);

var networkMonitorTMR:Timer = new Timer(10000,0);
networkMonitorTMR.addEventListener(TimerEvent.TIMER, checkNetwork);


function checkNetwork(e:Event = null):void
{
    networkMonitorXMLLoader.load(new URLRequest("//server/share/file.xml")); 
}

function networkMonitorXMLLoaderErrorHandler(e:Event = null) :void
{
    trace("Still Off");
}

function networkMonitorXMLLLoaded(e:Event = null) :void
{
    trace("Back On");
    rssTickerTMR.start();
    networkMonitorTMR.stop();
}

就像我说的,如果我切换&#34; // server / shar / file.xml&#34;到http://www.google.ca,它有效。当我拔掉我的以太网时,网络监视器启动,当我将其重新插回时,它会切换回RSS ...但是使用我的网络资源,它只会继续失败......

1 个答案:

答案 0 :(得分:2)

处理这类问题的一般模式是触发一个超时方法,该方法在重新建立之前一直检查连接。理想情况下,您将在后台执行此操作,以便用户不会注意到。在您的情况下,如果连接失败(IO_ERROR),请将计时器设置为在指定的时间量后重试下载xml文件的方法。如果失败,请再次设置。在此期间,您可以从头开始再次显示当前项目,并在下载新Feed后对其进行更新。