Calling ASP.NET assemblies from Flash
161
This code creates an instance of the ASObject object, named
aso
, and uses the
ASType
property
to identify the object as
Calculator
. The
Add
method inserts key-value pairs into the object.
Finally,
aso
is returned to Flash using the
Flash.Result
variable.
Handling the ActionScript object in Flash
The following ActionScript code handles the ActionScript object returned by the assembly:
import mx.remoting.Service;
import mx.remoting.PendingCall;
import mx.rpc.RelayResponder;
import mx.rpc.ResultEvent;
import mx.rpc.FaultEvent;
// Let the player know that we want an instance of com.mycompany.Calculator
// when any AMF packet contains a reference to Calculator
Object.registerClass("Calculator", com.mycompany.Calculator);
var dataService:Service = new Service(
"http://localhost/flashremoting/gateway.aspx",
null,
"FlashRemoting.ObjectTests.ObjectClass",
null,
null);
// Ask the server for some raw data...
var pc:PendingCall = dataService.returnObject();
pc.responder = new RelayResponder(this, "returnObject_Result",
"returnObject_Fault");
// Provide a callback for returnObject() when the data returns from the server
function returnObject_Result ( re:ResultEvent )
{
/*
Because the server code constructed an ASObject of type Calculator the
resultis a Calculator instance populated with the data specified in the
server code,wich makes the following cast and call valid.
*/
resultBox.text = String( Calculator( re.result ).add());
}
// Fault handler is omitted
The following is an ActionScript class file located in a package subdirectory structure starting in
the same directory as the FLA file that references it (for example, com/mycompany/
Calculator.as).
class com.mycompany.Calculator extends Object {
function Calculator() {
super();
x = 0;
y = 0;