Modified from original example by Sharriff Aina
I would explain with a very simple example how you can create Flash RPC applications using Web2py as a RPC service provider. I expect you to be familiar with the following topics as they are beyond the scope of this HowTo: Actionscript programming
Lets create a simple service that takes 2 numerical values, adds them together and returns the sum. We would call the service addNumbers
. Create a flash file using Adobe Flash (all versions as from MX 2004), in the first frame of the file, add these lines:
import mx.remoting.Service;
import mx.rpc.RelayResponder;
import mx.rpc.FaultEvent;
import mx.rpc.ResultEvent;
import mx.remoting.PendingCall;
val1 = 23;
val2 = 86;
service = new Service("http://localhost:8000/pyamf_test/default/run/amfrpc", null, null, null, null);
var pc:PendingCall = service.addNumbers(val1, val2);
pc.responder = new RelayResponder(this, "onResult", "onFault");
function onResult(re:ResultEvent):Void {
trace("Result : " + re.result);
txt_result.text = re.result;
}
function onFault(fault:FaultEvent):Void {
trace("Fault: " + fault.fault.faultstring);
}
stop();
The last thing to do would be to create a dynamic text field called txt_result
and place it on the stage.
As you have noticed, we have imported the mx remoting classes to enable Remoting in Flash, you can get them here. I would be using the Actionscript version 2 version of the classes. Add the path of the classes to your class path in the Adobe Flash IDE or just place the mx
folder next to the newly created file. Compile and export(publish) the swf flash file as pyamf_test.swf
.
Lets set up the gateway in Web2Py as we defined in our Flash file, create a new appliance, pyamf_test
, in the default
controller, create a function called gateway
and add these lines to it:
from gluon.tools import Service
service=Service(globals())
# expose all services
def call(): return service()
# define a Flax (AMF) service
@service.amfrpc
def addNumbers(val1, val2):
return val1 + val2
Next, place the pyamftest.amf, pyamftest.html, ACRunActiveContent.js, crossdomain.xml files in the static
folder of the pyamftest controller,fireup web2py using 8000 as the server port, point your browser to
http://localhost:8000/pyamf_test/static/pyamf_test.html
and see the result of the addNumbers
function displayed in the textfield.