Flash Remoting
I’ve been doing a lot of Action Script recently and needed a good library for making flash remoting calls. If you Google a bit you’ll find a very good one (for AS3) here. I’ve started with this one, but because I needed some additional features I’ve decided to create my own version.
Specifically, I wanted the ability to specify a time limit for remoting calls and have multiple ways of getting the result back (since it’s an asynchronous call). Here are some examples.
Connect the the remote service:
import com.onsysol.remoting.*;
var service:RemoteService = new RemoteService('http://myserver.com/gateway.php', 'MyService');
service.connect();
Make a remote call to MyMethod() and receive the result via event listeners:
service.addEventListener('onMyMethodResult', onMyMethodResult);
service.addEventListener('onMyMethodFault', onMyMethodFault);
service.MyMethod('param1', 'param2', 'etc...');
function onMyMethodResult(e:ResultEvent) {
trace(e.result);
}
function onMyMethodFault(e:FaultEvent) {
trace(e.fault);
}
In case you don’t want to use an event listener for every remoting call you make you can simply listen for a “default” event triggered after every remoting call that doesn’t have a specific event listener registered for it. So, instead of the above, you could have:
service.addEventListener(ResultEvent.RESULT, onResult);
service.addEventListener(FaultEvent.FAULT, onFault);
service.MyMethod();
function onResult(e:ResultEvent) {
trace('Result event received from ' + e.prop['methodName']);
trace(e.result);
}
function onFault(e:FaultEvent) {
trace('Fault event received from ' + e.prop['methodName']);
trace(e.fault);
}
And finally, if you don’t want to use events at all to get the response, you could use callback/anonymous functions, like this:
var prop:Object = {
onResultCallback: function(result:Object, prop:Object) {
trace('do something in your function...');
},
onFaultCallback: function(fault:Object, prop:Object) {
}
};
service.call('MyMethod', prop, 'param1', 'param2', 'etc...');
The default timeout is 2 seconds, but you can specify a new timeout value for all calls or on a per call basis:
// General timeout value, used for all calls that don't have a specific timeout:
RemoteService.TIMEOUT = 5000; // 5 seconds
// Make a remoting call with timeout set to 20 seconds:
service.call('MyMethod', {timeout: 20000}, 'param1', '...');
You can handle timeouts by either registering specific event listeners (onMyMethodTimeout), using the generic event (onTimeout) or using callback/anonymous function (onTimeoutCallback) in a similar way to how a result or a fault is handled.
Download the library here.
You can follow any responses to this entry through the RSS 2.0 feed. Responses are currently closed, but you can trackback from your own site.
Comments are closed.