Tutorial :New to FLEX - translating asynchronous comms from Java


I'm currently translating a Java project to Flex 3. A large part of the project involves asynchronous communications.

The program will connect to a stream, start downloading data. Shortly after connecting, it will need to download context data in the same format (a keyframe) by HTTP. At various points during the stream or keyframe, additional HTTP calls may be required, e.g. to get an encryption key.

In Java, I'd use a blocking call to perform the HTTP requests to get the key which would effectively pause the keyframe or stream while the HTTP is completing, which would then carry on where it was.

Unfortunately, Flex being single-threaded, and HTTP requests being implemented with asynchronous call-backs, it's impossible to implement the code in the same way.

Being new to Flex, I'm not sure what the normal practice would be for this. Currently, I'm planning to save the state and exit, relying on the HTTP completion to re-start the paused stream. Or maybe use timers ...

But for the stream, should I decouple the data being received from the processing it and buffer it while the HTTP requests are being processed? Or is it safe to ignore the progress events and let Flex and/or the OS buffer it?

Does anyone have any advice on an architecture which would make all this easier?


EDIT: Thanks for the answers so far ...

dirkgently - I'm not sure I understand what you're getting at, but I'll look into it.

brd6644 - one of the connections (the stream) is already a raw socket. The question was about pausing it and grabbing some other data from another source mid-stream.

CookieOfFortune - that looks useful, thanks.


Flex has a HTTPService class. From the looks of your problem description, I'd say create multiple such objects and bind them to different event handlers -- depending on the type of processing you want for a particular request.

<mx:HTTPService url="{myURL}" id="myHTTPData" method="GET"                   result="cacheData()" fault="downloadFault"                   resultFormat="object">    <mx:HTTPService url="{myURL2}" id="mySessKey" method="GET"                   result="saveSessKey()" fault="authFault"                   resultFormat="object">  

It is better to create a custom wrapper around the HTTPService and use these instead though. This will allow you to easily create objects with custom event handlers (and freeing them once done).


What about connecting via the raw ActionScript socket API? You can read your data and dispatch events as needed, with each event triggering a separate HTTPService call to go get the data.


When you use the HTTPService's send() method, an AsyncToken is returned. You can use the AsyncToken to synchronize your events.

private function streamResultHandler(event:ResultEvent):void  {      ...      for(var str : frame)      {          if(str == "getEncryptionKey")          {              var token:AsyncToken = keyHTTPService.send();              var tokenResult:Object = token.result;              var key = keyBuffer;              ...          }      }  }    private function keyHTTPServiceResultHandler(event:ResultEvent):void  {      keyBuffer = event.result;  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »