Flex Socket Dyanmically Changing Data

I am having trouble implementing socket using flex/actionscript. I have a server that does some calculations and displays data in csv format every 10 seconds. the server runs on unix platform.
Every 10 seconds the data is calculated and send thr socket to flex  The first line is always the header line followed by data. . Thedata is  then displayed in advanceddatagrid. I have everything working correctly except grabbing the entire data via socket.

The front end is in flex/action script. It connects to server . But in every run of onSocketData I only get the last part of the data. I cannot get the entire data that the server has sent. How can I loop thr in on SocketData function for the entire data that the server has sent and assign it to the label ( for now ) . so every 10 seconds the lbl will get refreshed with the new data. Currently it only has the data for the last snap.

Is there a way to increase the buffer size so that all the data I send comes together.

In the below code, basically for every 10 seconds run, the lbl.text should get recreated with new data . I have shown the label in the code below just for ease of reading instead of shwing the advancedatagrid.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()" layout="vertical">


<mx:Script>
<![CDATA[

import mx.controls.Alert;
import flash.display.Sprite;
import flash.events.ProgressEvent;
import flash.net.Socket;
import flash.utils.ByteArray;
import flash.errors.EOFError;
import mx.collections.ArrayCollection;

public var srv:Socket = new Socket();
public var dataRecv:String = new String();




public function init():void {

srv = new Socket();
srv.addEventListener(Event.CONNECT, onConnect );
srv.addEventListener(IOErrorEvent.IO_ERROR,onError );
srv.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData );
srv.connect( "localhost", 8000 ) ;
srv.flush();

}

private function onConnect ( event:Event ) : void {
Alert.show ( "Socket Connection Sucessfull" ) ;
}

private function onError ( event:Event ) : void {
Alert.show ( "Socket Connection Failed " + event.toString() ) ;
}


private function onSocketData ( event:ProgressEvent ) : void {

var buffer:ByteArray = new ByteArray();

srv.readBytes(buffer, buffer.length, srv.bytesAvailable);

lb1 = buffer.toString()
// this is where tthings go wrong. The srv.bytesAvailable only processes certain amount of data and I only get the last processed data

}

]]>
</mx:Script>
<mx:Label id="lb1"/>
</mx:Application>

Open in new window

DeveloperLearningAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

IqAndreasCommented:
First of all, are you supposed to be concatenating onto the "lb1" variable instead of replacing it each time there is data?
lb1 += buffer.toString();

And I believe the reading of bytes would go a lot faster if you use this command instead:
lb1 += srv.readUTFBytes(srv.bytesAvailable);

I don't believe you even need to create the new ByteArray instance every time there is socket data.

Does your problem still remain after these fixes? Just ask if you want me to explain better or if it still isn't working as planned.
DeveloperLearningAuthor Commented:
lbl += sv.readUTFBytes ( srv.bytesAvailable )  will work but only for the first run. When the data is published again after next 10 seconds it gets appended. In that case it should clean lbl and then get appened. If I clean lbl then, I get only the last data snap.
IqAndreasCommented:
Would it work to set a timer up to clear the label every 10 seconds? (see attached code. Just add it in with your existing code)

Could you elaborate a little more on what this data contains and what you are doing with it? Then it might be easier finding a good solution.
var timer:Timer = new Timer(10000); //10,000ms = 10 seconds
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();

function onTimer(ev:TimerEvent)
{
   //Clear the textField each time the timer ticks
   lb1.text = "";
}

Open in new window

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

IqAndreasCommented:
Give this code a try and see if it suits your needs. Basically what it does is clear out half of all the code at a time.

But if it is not what you are looking for, please elaborate more on the data that is coming from the server. Does it contain line breaks?
Is it only sent every 10 seconds, and that's why you want to clear the old data out?
Do you have control of this server side script that is sending out the data?

The IDEAL method would be to have the server side script send out a "end of data" character or something that tells the function it has received everything it needs. Because, even if your server sends all your data in one "process", it arrives into Flex divided up into several bits and chunks.

You need some way of knowing when it's the end of a "chunk" of data.
import mx.controls.Alert;
import flash.display.Sprite;
import flash.events.ProgressEvent;
import flash.net.Socket;
import flash.utils.ByteArray;
import flash.errors.EOFError;
import mx.collections.ArrayCollection;


var timer:Timer = new Timer(5000); //5,000ms = 5 seconds
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();

function onTimer(ev:TimerEvent)
{
   //Each time the timer ticks, clear away half of all the text,
   //And move up the other half that's newer
   oldData = newData;
   newData = "";
}


public var srv:Socket = new Socket();

public function init():void {

    srv = new Socket();
    srv.addEventListener(Event.CONNECT, onConnect );
    srv.addEventListener(IOErrorEvent.IO_ERROR,onError );
    srv.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData );
    srv.connect( "localhost", 8000 ) ;
    srv.flush();

}

private function onConnect ( event:Event ) : void {
    Alert.show ( "Socket Connection Sucessfull" ) ;
}

private function onError ( event:Event ) : void {
    Alert.show ( "Socket Connection Failed " + event.toString() ) ;
}

var oldData:String = "";
var newData:String = "";

private function onSocketData ( event:ProgressEvent ) : void 
{
    newData += srv.readUTFBytes(srv.bytesAvailable);
    
    lb1 = oldData + newData;
}

Open in new window

FlassariCommented:
I think you're going to have to send a special END tag or byte from the server in the end of a data message and listen to it in flash so you know when to clear the previous data, otherwise you could end up clearing it in the middle of a download.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DeveloperLearningAuthor Commented:
Thx I did this by adding  START_MESSAGE and END_MESSAGE before and end of my message.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Flash

From novice to tech pro — start learning today.