How do I cancel an AMF Call with NetConnection?

694 views Asked by At

Let's say that I have a NetConnection Object inside a Singleton Pattern that is shared with all Data Access Object classes throughout all my application. Now, you went on a screen and asked to load 5 thousands of records because you want to search for some mistake or whatever. But now you just realized that you're in the wrong screen and you actually wants 5 thousand of records from another "Entity". How to prevent you from having to wait almost 2 minutes until the data gets here so you can move on to another Screen and ask for some different data? I tried using ESC button as a key caller to the NetConnection.close() event, but it actually doesn't work. It just dispatch an empty event and the call still returns. I know that I can't ask the server not to keep doing his job, but I wanna know how I can make sure that after you pressed ESC or something like that, the ActionScript won't bother with that specific call anymore and you can move on with your life. And, when the server respond to that call, in the background, I'm going to throw it away because you already agreed to cancel it.

Ps.: I don't have any doubt on how to listen to ESC button and call some method through it, I just want to know what to call once the user decided that he doesn't want to receive that data anymore and he/she wants to move on without having to close the App and open again.

Edit:

I tried to destroy the Responder object after making the call, but it didn't work.

        public function call(command:String, f:Function, fault:Function, ...parameters):void{
        var r:Responder = new Responder(f, fault);
        this._Responder = r;
        if(parameters.length == 0)
            GATEWAY.call(command, _Responder);
        else
            GATEWAY.call(command, _Responder, parameters);
    }

    public function close():void{           
        _Responder = new Responder(function(obj:Object):void{}, function(obj:Object):void{});
        //_Responder = null;
        trace("Canceled");
    }

with the code described up here, I stopped making calls to NetConnection::call and started to call the "Call Function" that I wrote. So this function would build a Responder that will be in the class scope so I could access it even after the call has been made and the ESC button would call the "Close" function. I tried setting it to null and re-building it. It didn't work. I think after the NetConnection makes the call, the Responder cannot be changed.

1

There are 1 answers

2
Mohammad Haseeb On

The NetConnection class does not have support for cancelling AMF calls. However, like shaunhusain said, you can keep storage (array, dictionary whatever) of the methods being called from your singleton class and once you cancel a call, it should disable the result event listeners for that call only.

So, even if you exit the module that is waiting for the result from the AmfCall, there will be no callbacks from your singleton class and you can continue to the next screen.

public class Call
{
var responder:Responder;
var service:String;
var params:Array;

public function Call(service:String, params:Array)
{
   responder = new Responder(this.onResult, this.onFault);
   this.service = service;
   this.params = params;

}

public function removeListeners():void
{
   removeEventListener(CallEvent.RESULT, onResult);
}

public function onResult(result:Object):void
{
   this.result = result;
   dispatchEvent(new CallEvent(CallEvent.RESULT, callId, result));
}

}

The Call class is responsible for dispatching events on receiving a result. However, if there are no event listeners, the events dispatched will not be listened by any class.

This code snippet is taken from AmfPhpToolBox. You might want to use that library instead to tackle all these issues.

AMFPHPToolBox