I’m at a lose end and would really appreciate some help as it is way out of my comfort zone with as3. Basically I have a sales presentation showing on a screen in various offices where I work. These show daily sales figures that can change as and when somebody makes a sale. I have an swf loading data from an XML file into slides and everything works great apart from if the XML file changes at the moment I need to force a refresh (manually). I’d like to make it so that if the file changes it automatically reflects in the swf. So far my research has pointed me to AS3 Socket Communication but I haven’t been sucessful in finding a relevant tutorial.My code is below, if anyone could point me in the right direct i’d greatly appreciate it.
//
//XML LOADING
//
var xmlfile:String;
var xmlf:String = stage.loaderInfo.parameters.xmlfile;
if(xmlf!=null){
xmlfile = xmlf;
}else{
xmlfile = "file path";
}
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, function(evt:IOErrorEvent):void {},false,0,true);
xmlLoader.addEventListener(Event.COMPLETE, xmlLoadingDone);
xmlLoader.load(new URLRequest(xmlfile));
////////////////////////////////////////////////////////////////////////////////////////////
//
//socket connection text
//
var xmlSocket:XMLSocket = new XMLSocket();
var hostName:String = "hostname";
var connectionOpen:Boolean = false;
xmlSocket.addEventListener(Event.CONNECT, onSocketConnection, false, 0, true);
xmlSocket.addEventListener(DataEvent.DATA, onSocketResponse, false, 0, true);
xmlSocket.addEventListener(Event.CLOSE, onSocketClose, false, 0, true);
xmlSocket.addEventListener(IOErrorEvent.IO_ERROR, onIOError, false, 0, true);
function onSocketConnection(evt:Event):void {
//option to go here
connectionOpen = true;
trace("connection open");
}
function onSocketResponse(evt:DataEvent):void {
//option to go here
}
function onIOError(evt:IOErrorEvent):void {
//option to go here for error
}
function onSocketClose(evt:DataEvent):void {
//option to go here
xmlSocket.close();
connectionOpen = false;
}
///////////////////////////////////////////////////////////////////////////////////////////
//
//XML DATA PARSING AND IMAGE LOADING STARTER
//
function xmlLoadingDone(e:Event):void {
xmlLoader.removeEventListener(Event.COMPLETE, xmlLoadingDone);
var XMLdata:XML = new XML(e.currentTarget.data);
}
You will need to a polling loop, basically set up a
Timerand have the elapsed timer handler reload the XML.Make the loop interval as long or as short as you think necessary.
Update:
This code sample can be added to your existing code…
That should do what you need.