To be accurate, that is not "synchronous" behavior, which is about
impossible in Flex.

 

But it is a fine way to handle onData events, since sockets do not give
you the AsyncToken to use for this, as do the RPC objects.

 

Tracy

 

________________________________

From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of e_baggg
Sent: Thursday, December 07, 2006 8:03 AM
To: [email protected]
Subject: [flexcoders] Re: socket question

 

David, 
We are able to have some synchronous behavior to sockets. What we do
is assign a unique ID with each request the client sends, and when the
server is done, it passes that id back.

So to further illustrate your example:
Client sends this:
<getServerData id="1" data="yourData"/>
The server would then send back:
<yourData id="1">blahblahblah</yourData>
<done id="1">

Then, on your onData() method, you can check all <done/> XML, get the
ID, and then use that ID to look up a callback function that you
registered with that ID.

Basically, all of our requests go through a Command which implements
the IResponder class, we register that class to a Singleton which
holds all the callback with the "id"...and when we see "<done/>", we
check to see if a callback is there and if so it calls it's method.
This ensures that all the data is back we want. Even if your
getServerData request returns multiple socket messages:

<getServerData id="1" data="yourData"/>
<yourData id="1">blahblahblah</yourData>
<yourData id="1">blahblahblah22222</yourData>
<yourData id="1">blahblahblah333333</yourData>
<done id="1">

Hope this helps.
-Kevin

--- In [email protected] <mailto:flexcoders%40yahoogroups.com>
, "David Buitenveld"
<[EMAIL PROTECTED]> wrote:
>
> Hi all -
> 
> I am creating a server interface, one of whose implementations
> involves talking over a socket connection. I have a need for
> synchronous behavior - I call an API method, say
> getServerData(aParam), and I get back the result that I want. Not sure
> what a good way to do this with sockets would be since the receive
> data event makes them asynchronous in nature - can anyone point me
> to a good tutorial, or provide a hint?
> 
> Consider a made up example :
> 
> class SocketServer implements Server 
> private var _lastResult:String;
> 
> // client calls this method and expects a result passed back
> public function getServerData(aParam:String): String {
> _socket.addEventListener(ProgressEvent.SOCKET_DATA, onData); 
> connectToSocket(someHost, somePort);
> 
> _lastResult null;
> issueCommandViaSocket(aParam);
> 
> // sit around until we get data
> while (_lastResult == null) ; 
> return _lastResult;
> }
> 
> private function onData(e:ProgressEvent):void {
> _lastResult= socket.readUTFBytes(socket.bytesAvailable);
> }
> 
> this works conceptually, except that the wait around part hangs the
> app.. if I remove that, then I can get the return value and log it in
> the onData method.. so, am I even on the right track here? Is there
> some way to have actionscript pause or wait until the desired event
> gets fired?
> 
> thanks for any thoughts -
> 
> david
>

 

Reply via email to