Hi there,

I have a fairly simple idea and need help:

I want to build a simple socket connection (not XMLSocket!) to a server to exchange simple text based commands. The Flash Client sends a text command, the server answers with a response (text based). While this is very easy in e.g. C# I run into "threading" issues with Flash. Somehow it seems impossible to wait for the server response.

Why? Obviously the entire scenario is asynchronously. I have extended the Socket class and added a function for the data receive event:

aDispatcher.addEventListener(ProgressEventType.SOCKET_DATA, onDataReceived);

In the onDataReceived() I read all incoming data (via readUTFBytes(_length)) parse it and put it into a responseQueue (an Array).

Now comes the difficult part in question here:

I want to create a function SendCommand(cmd:String):String which takes a Command string as a parameter and returns the result.

So basically the function should look somewhat like this:
function SendCommand(cmd:String):String
{
writeUTFBytes(cmd);
var result:String = undefined;
// code to wait for result and assign it
return result;
}

In another area of the application I want to call it like this:

response = SendCommand("login name");
Alert.show(response),

Here is the problem:
SendCommand() is called, sends the data and immediately returns.
The returned response is undefined. Alert shows "undefined".
THEN the dataReceive event fires. Response gets processed.
However, there seems to be no way of waiting (idle) right after the writeUTFBytes call.
I have tried everything. If I do a long lasting ro loop, it seems as if Flash handles it single threaed, so the OnReceiveData Event gets fired immediatley after my idle for loop finishes.

Any idea?

 

Ralf

 



--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com




YAHOO! GROUPS LINKS




Reply via email to