Hi David,
I tried doing something similar to this a while back but without
success. Others have also noted that there is no way to do thread-
blocking in Flex. The problem is that your onData method won't even
get called until next time through the Flex event loop; and that's
not going to happen until after getServerData has returned. So
unfortunately(?) it doesn't work to hang around in a loop until the
service call returns. I think you might be disappointed with this one.
Cheers,
Lach
On 07/12/2006, at 11:00 AM, David Buitenveld 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