How to get/process the Data from XMLSocket?
Hello All,
I'm using Adobe Professional CS6, and Actionscript 3.0.
To start with I have a C# program that basically just feeds data out from a Server in the form of XML data. I then have a Flash program that should receive the data and then I'll do some stuff with it. We had this working in Actionscript 1.0 but we need to upgrade the code to Actionscript 3.0, but instead of just trying to convert stuff over to the new Actionscript version, I'm going to re-write the program.
So far I've basically just defined some EventListeners most of which I found examples of online for using XMLSockets. As of now the Flash program will connect an XMLSocket to the Server that is feeding out the data, and the server feeding the data resends new data every 5-10 seconds or so.
So when a grouping of XML Data comes in on the port I defined, the following Function gets executed for handling the incoming data. For my EventListener's Function for "socket.addEventListener(DataEvent.DATA, dataHandler);" ("socket" is defined as --> var socket = new XMLSocket(); ). So when the new data comes in it executes the above function.
The trouble I'm having is declaring a new variable of type "XML" and then using that variable to pull out individual "nodes/children" from the incoming XML Data. I found an example online that was very close to what I want to do except instead of using an XMLSocket to get data constantly streaming in they use the "URLLoader" function to get the XML data. I know I'm receiving the XML data onto the server because if I set the (e: DataEvent) variable defined in the function "head" to a string and then run a trace on that I can see ALL of the XML data in the "Output Window".
But I can't seem to be able to set (e: DataEvent) to a XML variable so I can access individual elements of the XML data. The example I found (which uses the URLLoader instead) uses this line (myXML = new XML(e.target.data);) to set the XML Variable, which won't work for mine, and if I try to do the same thing in my code it simply prints this for as many lines as there is XML data --> "[object XMLSocket]"
MY CODE:
*I left out the other Functions that are in my code for the EventListeners you'll see defined below, except for the one in question:
---> "function dataHandler(e: DataEvent):void"
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.events.DataEvent;
var socket = new XMLSocket();
socket.addEventListener(Event.CONNECT, connectHandler);
socket.addEventListener(Event.CLOSE, closeHandler);
socket.addEventListener(DataEvent.DATA, dataHandler);
socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
socket.addEventListener(ProgressEvent.PROGRESS, progressHandler);
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
var success = socket.connect('server.domain.name.local',3004);
/*
The code in this function was used from an example I found online as described above in the post:
LINK --> http://www.republicofcode.com/tutorials/flash/as3xml/
*/
// The Commented out code (*in blue) will show ALL the xml data inside the "Output Window":
function dataHandler(e: DataEvent):void {
// var myStr:String = e.data;
var xml:XML;
xml = new XML(e.target.data); //<--- THIS DOESN"T WORK (I DONT THINK 'target' IS CORRECT HERE)??
if (socket.connected)
{
// trace(myStr)
trace(xml);
}
}
The Output from the line "trace(xml)" will show this data below in the "Output Window" (*FYI There should be 6 lines of XML Data on each 'update'):
[object XMLSocket]
[object XMLSocket]
[object XMLSocket]
[object XMLSocket]
[object XMLSocket]
[object XMLSocket]
Could someone show or point me in the right direction on this. I want to be able to access specific parts of the incoming XML Data.
Here's some sample XML Data:
<MESSAGE VAR="screen2Display" TEXT="CSQ_1" />
<MESSAGE VAR="F1_agentsReady" TEXT="111" />
<MESSAGE VAR="F1_unavailableAgents" TEXT="222" />
<MESSAGE VAR="F1_talkingAgents" TEXT="333" />
<MESSAGE VAR="F1_callsHandled" TEXT="444" />
<MESSAGE VAR="F1_ABDRate" TEXT="555" />
Any thoughts or suggestions would be greatly appreciated..!
FYI:
I'm VERY new to Actionscript/Flash Developing (*about a week now), so go easy on me haha...
Thanks in Advance,
Matt