Skip to main content
Known Participant
April 19, 2013
Answered

How to get/process the Data from XMLSocket?

  • April 19, 2013
  • 2 replies
  • 3242 views

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

This topic has been closed for replies.
Correct answer Andrei1-bKoviI

Based on DataEvent documentation

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/DataEvent.html

looks like you need to read data property on the event:

var xml:XML = new XML(e.data) - not e.target.data

2 replies

sinious
Legend
April 20, 2013

Edit:

D'oh beat me to it. Yes the data is in e.data. The documentation also mentions what it expects from your server in XMLSocket:

  • XML messages are sent over a full-duplex TCP/IP stream socket connection.
  • Each XML message is a complete XML document, terminated by a zero (0) byte.
  • An unlimited number of XML messages can be sent and received over a single XMLSocket connection.

Check the e.data property and make sure your server is meeting that requirement. You're getting data so I assume you've already understood past later versions of FP9 you need socket policy files if you're not using AIR.

They also only recommend using URLLoader if your application server isn't compatible with AS3's XMLSocket or does not require real-time communication. If you're willing to just keep pulling from a server rather than the server pushing it to you then you can use URLLoader. Sounds like you want real-time though so I would stick with getting XMLSocket to work.

mrm5102Author
Known Participant
April 22, 2013

Also, to add to the post above, I just commented out:

          //trace(xml)

And added the following:

          trace(e.data)

After I added that line, I can now see the XML Data in the "Output Window". So I can say with certaintity that e.data DOES contain

the XML Data being pushed out from the remote server.

So now I just need to figure out how to access specific sections of the data using that variable we created called "var xml:XML = XML(e.data);".

What should I look up for finding out methods for doing that? Would it be "XMLSocket" methods, or "XML Variable" Data Type methods, etc..?

P.S.:  Sorry for the extra long reply....

Thanks Again,

Matt

mrm5102Author
Known Participant
April 22, 2013

Hey Guys, back again..... With better news this time haha!

Go it working...!! Victory!

Looks like most of the code in the Actionscript I had was correct. Except that the XML Data I was receiving looks

like it needed to be surrounded by an extra, lets call it "Parent" Node, in order to dig into the XML Data it received

and process it...

I changed the XML Data being passed to this:

<FRAME>

    <MESSAGE VAR="screen2Display" TEXT="Call_Queue_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" />

</FRAME>

You'll notice I added the extra <FRAME> tags to the XML Doc, and I am now able to trace the "xml" variable and see

it in the "Output Window".

My "working" commands and their output are now:

     *Note that the data being passed is just test data so I get the same message over and over again, but it will work for this example:

function dataHandler(e: DataEvent): void

{

    

     var xml:XML = XML(e.data);

     if (socket.connected)

     {

          trace("Data Received....");

          trace(xml.MESSAGE[0].@TEXT);

          trace("Agents Ready = " + xml.MESSAGE[1].@TEXT);

          msgArea.htmlText += "*socket.connected = TRUE\n";

     } else {

          msgArea.htmlText += "*socket.connected = FALSE\n";

     }

And the OUTPUT is:

Data Received....

Call_Queue_1

Agents Ready = 111

So I think I should be able to figure the rest out.

Thanks you guys so much for helping me out with this, VERY MUCH APPRECIATED!!

Thanks Again,

Matt

Andrei1-bKoviICorrect answer
Inspiring
April 20, 2013

Based on DataEvent documentation

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/DataEvent.html

looks like you need to read data property on the event:

var xml:XML = new XML(e.data) - not e.target.data

mrm5102Author
Known Participant
April 22, 2013

Hey Guys, thanks ALOT for the replies!!

Andrei1 and Sinious,

The code I show above is actually just what I found in an example online, and its what i was trying to show you what "they" used

in the URLLoader example. I wasn't trying to use URLLoader, but it was the closest example I found online to  what I was trying to

do in terms of processing the XML Data, and not how I read it in. So XMLSocket IS definetly what I'm trying to use because the Server

I'm receiving the XML Data from "pushes" it out every 10 seconds or so. So yea I definatly want to use XMLSocket and not URLLoader...

Sorry for the confusion...

The example you show:

     var xml:XML = new XML(e.data)

Is actually what I had in my original code but I couldn't get it to show anything in the "Output Window" using the trace() method on

it, so I assumed I wasn't doing it correctly...

What my original code was in the "dataHandler()" Function was this:

function dataHandler(e: DataEvent): void

{

    var xml: XML = XML(e.data);

    if (socket.connected)

    {

        trace(xml);

        msgArea.htmlText += "*socket.connected = TRUE\n"

    } else {

        msgArea.htmlText += "*socket.connected = FALSE\n"

    }

}

OUTPUT:

And what I get when I run a Debug on it is, in the "msgArea", which is just a simple Text Box w/ Dynamic Text on the main frame it prints:

    "socket.connected = TRUE

     socket.connected = TRUE

     socket.connected = TRUE

     socket.connected = TRUE

     socket.connected = TRUE

     socket.connected = TRUE"

It prints that message 6 times in the msgArea for each XML Message that comes in (*which I think 6 times because there

are 6 "sections/nodes" in the XML Data). But then NOTHING prints in the "Output Window" for the "trace(xml)" command.

Not sure why nothing is showing in the "Output Window", maybe the XML is not complete? But I could see data in the "Output

Window" when I set "e.data" to a string variable, so I know the Data is reaching the program. Since I could see the XML Data in

a string variable, does that mean I can rule out that the XML Data coming in is considered a "complete" XML Document?

Because I wasn't sure if to be considered a complete XML Document it would need to have this included in the message..?

                    "<?xml version="1.0"?>"


For the tests I'm running, one single XML message which is being pushed out from the server looks like this:

<MESSAGE VAR="screen2Display" TEXT="Call_Queue_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" />

Would I need to include that "Header" in the XML message above?

               i.e. "<?xml version="1.0"?>"

Or do I need to be more specific in the trace command instead of just using "trace(xml)"?

Also yes, I already have a Flash Policy file setup and working correctly.

Anyway, thanks again guys for the replies, very much appreciated!!

Thanks Again,

Matt