2

I've been tasked with the creation of an AS3 client for an HTML5 WebSocket server.

My initial thinking is to create this service using some of the existing classes in AS3, namely attempting to emulate RemoteObject, but having looked into it, I can't really see the point of the ASyncToken!

I understand the token is used as some form of reference between the server call and its response, but I just can't see where/how it becomes a unique identifier.

Take this short piece of code for example:

 var token:AsyncToken = myService.myCall(params);
 news.addResponder(new Responder(onResult, onFault));

 function onResult(event:ResultEvent) {
   // do stuff
 }

 function onFault(event:FaultEvent) {
   // do stuff
 }

Obviously the token has its own responder, and the service can keep a dictionary of tokens-messages, but if I were to call myService.myCall twice, before either responds, would the service know which token to associate the response with? Does the service create a unique identifier to associate the response back to the token, or would it fail under these circumstances?

If it does internally store its own unique identifier for calls, how would it be any better than skipping the token entirely and passing the responder in the call parameters? E.g.

 myResponder = new Responder(onResult, onFault);
 myService.myCall(myIResponder, <additional params>);

 function onResult(event:ResultEvent) {
   // do stuff
 }

 function onFault(event:FaultEvent) {
   // do stuff
 }
adamk
  • 439
  • 3
  • 10
  • Just looked at my own example code and can see the token isn't stored in the service at all, so the token is a reference to the call rather than the other way around. I still don't see where it differentiates between two calls to the same method though.. – adamk Jan 11 '11 at 22:02
  • Docs mention this: "This class provides a place to set additional or token-level data for asynchronous RPC operations. It also allows an IResponder to be attached for an individual call. The AsyncToken can be referenced in ResultEvent and FaultEvent from the token property." – Heikki Jan 11 '11 at 22:36

3 Answers3

2

Two things stand out:

  1. You can assign more than one responder to an AsyncToken;
  2. You can bind the result of the AsyncToken.

You might find these two things useful depending on what kind of data you're working with... Also there might be more to it than this.

bug-a-lot
  • 2,446
  • 1
  • 22
  • 27
  • Thanks for the response, that does make sense. I guess the bindable result is the most useful part. Time to run some testing.. – adamk Jan 13 '11 at 15:20
0

For more complicated applications, the AsyncToken lets you have a bit more flexibility in what functions/closures/methods to the reception of data, or handling errors.

mezmo
  • 2,441
  • 19
  • 22
-1
public function mssqlQuery(sql:String,fid:String) : void {
    var http:HTTPService = new HTTPService;
    var parm:Object = new Object;
    parm.fas_sql = sql;
    parm.fas_db = mssql_db; 
    http.url = mssql_url+"?irand="+Math.random();
    http.showBusyCursor = true;
    http.request = sql;
    http.addEventListener(ResultEvent.RESULT, mssqlResult);
    http.addEventListener(FaultEvent.FAULT, mssqlFault);
    http.method = "POST";
    sqlToken = http.send(parm);
    sqlToken.param = fid;
}
HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
  • 1
    When giving an answer it is preferable to give [some explanation as to WHY your answer](http://stackoverflow.com/help/how-to-answer) is the one. – Stephen Rauch Feb 10 '17 at 02:33