Link to home
Start Free TrialLog in
Avatar of DeveloperLearning
DeveloperLearning

asked on

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

Avatar of IqAndreas
IqAndreas
Flag of Sweden image

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.
Avatar of DeveloperLearning
DeveloperLearning

ASKER

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.
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

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

ASKER CERTIFIED SOLUTION
Avatar of Flassari
Flassari
Flag of Iceland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thx I did this by adding  START_MESSAGE and END_MESSAGE before and end of my message.