1

我正在创建一个 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;
}

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

编辑!!!

正如 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();
}

就像我说的,如果我将“//server/shar/file.xml”切换到http://www.google.ca,它就可以工作。当我拔下以太网时,网络监视器启动,当我将其重新插入时,它切换回 RSS ......但是使用我的网络资源,它只是继续失败......

4

1 回答 1

2

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

于 2012-08-30T20:29:38.307 回答