Archive for the ‘Flash remoting’ Category

AMFPHP with ActionScript 3 101

July 20, 2007

Client-side:

1. Use flash.net.NetConnection to call server-side function this way:

var conn:NetConnection = new Connection();

conn.connect(http://www.mysite.com/amfphpgatewaydir/gateway.php);

conn.call(“com.projectA.ClassName.getCash”, new Responder(onResult, onFault), param1, param2);

In the call() function of Connection object, the first parameter is the method “identifier” of type String in the form of dot-delimited packagePath.className.methodName; the second parameter is a Responder object that takes two parameters with the first one onResult handler and the second one onFault handler (both takes one paramter of tyep Object); the rest/consecutive parameters are the parameters that are taken by the first remote method call.

2. Implement onFault() and onResult() for the remote method call. onFault() always take a type Object parameter against which you can still reflect by the old-good-day “for-property-in-object” style loop. onResult() takes the returned function call result from the remote method. It can be complex type such as Object or native AS3 dataTypes such as String or Array. A server-side defined class needs to have its corresponding AS class and is deserialized as “associative” array by onResult().

Below is a snippet AS of working example:


package test
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.system.System;
import flash.net.NetConnection;
import flash.net.ObjectEncoding;
import flash.net.Responder;
import flash.utils.describeType;
import packageName.ClassName;
public class AMFPHPTest01 extends Sprite
{
private var _foo:TextField;
private var gatewayURL:String = "http://www.mysite.com/amfphpservice/gateway.php";
private var connection:NetConnection;
public function AMFPHPTest01():void
{
inititialize();
}
private function inititialize():void
{
_foo = new TextField();
_foo.text ="info";
_foo.autoSize = TextFieldAutoSize.LEFT;
addChild(_foo);
//
init();
}
private function init():void
{
_foo.text = "TotalMemory: "+System.totalMemory;
connection = new NetConnection();
//connection.objectEncoding = ObjectEncoding.AMF0;
connection.connect(gatewayURL);
connection.call("packageName.className.methodName", new Responder(onResult, onFault), 1);
}
private function onResult(evt:Object):void
{
_foo.text = "onResult!";
reflect(evt);
}
private function onFault(obj:Object):void
{
_foo.text = "onFault!\n"+"obj: "+obj.error;
reflect(obj);
/*
description: The class {*} could not be found under the class path {*}
level: User Error
details: */BasicActions.php
code: AMFPHP_FILE_NOT_FOUND
line: 33
*/
var description:XML = describeType(obj)
//will output an xml object
trace(description..accessor.@name.toXMLString());
}
private function reflect(obj:Object)
{
for(var i in obj)
{
_foo.appendText("\n"+i+": "+obj[i]);
//trace(i+": "+obj[i]);
}
}
}
}

Server-side:

1. No need to define a method table to expose services to the gateway any more. Just declare those methods as public;

2. Use _explicitType as a class member to map a php class to an AS one like this in a php class definition.

"var $_explicitType = "packageName.ClassName“;

Advertisements