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