سؤال

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
 }
هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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.

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;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top