Using the AMFPHPConnection class

In this post I’ll be talking about a class I’ve made called AMFPHPConnection that extends Flash’s NetConnection object. This class eases and enhances the process of using AMFPHP for Flash remoting. But before we begin, just a few notes: I’m not the usual author, and you’ll need to know how to use AMFPHP the normal way, or you may not get some of the terminology. If you want to learn how to use it, I have a series on my YouTube channel for that purpose, go here to watch it: http://www.youtube.com/user/BlackBulletIV#grid/user/CAD2DAE46A04939D

First go and download the class from here: http://www.nova-fusion.com/cms/FileFolder/AMFPHPConnection.zip

This zip file contains the minimized class in it’s package folders: com/novafusion/net/AMFPHPConnection.as. It also contains readable version of the class, which has documentation, comments and more whitespace; it is: AMFPHPConnectionReadable.as. If you actually want to use this class instead, rename it to AMFPHPConnection.as and place it in the proper package folders. The zip file also contains package details similar to what I’m talking about now, and instructions similar to what I’m going to be talking about below. Now let’s get onto how to use it.

Setup

To get started, import the class:

1
import com.novafusion.net.AMFPHPConnection;

Next create a new instance, you can call the variable name whatever you like but I’ll go with connection:

1
var connection:AMFPHPConnection = new AMFPHPConnection("http://localhost/amfphp/gateway.php");

When you create a new instance, you are expected to pass in one parameter, the path to your AMFPHP gateway.php file. When you create the instance the class will automatically set up a connection and a responder for your function calls. If you would like to change the gateway location at any point do this:

1
connection.gateway = "http://www.some-other-server.com/amfphp/gateway.php";

This will set the gateway location in the class and reconnect to that server. Access the path to the current gateway this way:

1
connection.gateway

Quick Service Referencing

Quick service referencing helps a lot in speeding things up and reducing the amount of code you have to right. If you have a service you use a lot in your code, the connection class can store it for you and cut down how much you have to type to reference it. Examples of referencing strings are:

1
2
3
4
"FlashPHPTest"; // just a class name, but this could be a folder name
"UsingAMFPHP.TalkBack"; // folder and a class inside it
"UsingAMFPHP.TalkBack.put_together"; // folder, a class inside it, and a method inside the class
"FlashPHP" // part of a class name (could be folder or method name)

You can basically store any string in them, and reference it quickly when calling a service. You could use each of these reference strings in calls like this:

1
2
3
4
"$1.spitBack"; // just a class name, but this could be a folder name
"$2.put_together"; // folder and a class inside it
"$3"; // folder, a class inside it, and a method inside the class
"$4Test.spitBack" // part of a class name (could be folder or method name)

You use your references with a dollar sign and then it’s number (which is assigned to it in the order you add them to the class). As you can see this will greatly reduce what you have to write in your code.

You can add service references in two ways. The first is to add them in when constructing the class:

1
var connection:AMFPHPConnection = new AMFPHPConnection("http://localhost/amfphp/gateway.php", "FlashPHPTest", "UsingAMFPHP.TalkBack");

The second way is to add them in with a method provided:

1
connection.addServices("FlashPHPTest", "UsingAMFPHP.TalkBack");

You can add as many as you like at one time, and use the method as many times as you like. You can also remove service references like this:

1
connection.removeServices("FlashPHPTest", "UsingAMFPHP.TalkBack");

It’s not recommended that you remove service references, as it might generate confusion as to the order in which to reference them afterwards (just more for you to keep track of).

Calling Services

To call a service, use this method and pass in your service and parameters (not the responder! That’s taken care of for you):

1
connection.talk("SomeService.someMethod", "Some parameter", "Another parameter");

The function is called talk because of confliction with the NetConnection object’s call method. However you can still use that method, like this:

1
connection.call("SomeService.someMethod", null, "Some parameter", "Another parameter");

You can pass in the responder either as null (the object’s responder object will be used instead, which is what you want), use connection.responder (replace connection with your instance name), or pass in your own custom responder (note that result and fault listeners won’t work).

You can use quick service referencing in your calls like this:

1
connection.talk("$1.spitBack", "Hello world!");

Just use strings in the style shown in the quick service referencing section and all will be well.

Result and Fault Listeners/Handlers

We need to see up some listener/handler functions to respond to results and faults from AMFPHP:

1
2
3
4
5
6
7
8
function onResult(event:Event):void {
// do whatever
trace(connection.result);
}
function onFault(event:Event):void {
// do whatever
trace(connection.fault);
}

Notice that instead of receiving objects they actually receive plain event objects; that’s because they’re triggered by custom event listeners. You access the last result object with connection.result (replace connection with your instance name) and the last fault with connection.fault (replace connection with your instance name). Now how do we add the listeners for these functions? Here’s how:

1
connection.setListeners(onResult, onFault);

Just pass in the result function and the fault function and they’ll be triggered for their respective events. Now you don’t have to set either one, if you want to set the fault handler but leave the result handler as it is just leave the onResult function as null. Now be careful about these handlers, when you set them like this they become the default handlers for all calls. One way to get around this is to remove the listeners in the on result or on fault functions, using this:

1
connection.removeListeners();

You could do that, but there’s a better way to do it. Set the third parameter of setListeners() to true; this will make these listeners only last for one call, and on the next result or fault they will be removed. Here’s how:

1
connection.setListeners(onResult, onFault, true);

You could also add listeners the harder way like this:

1
2
connection.addEventListener(AMFPHPConnection.RESULT, onResult); // For results
connection.addEventListener(AMFPHPConnection.FAULT, onFault); // For faults

And of course to remove them you just use them same code, just change the function to removeEventListener().

Conclusion

Hope that helps! Here’s a sample application to give you some perspective on how this class lessens your code when using AMFPHP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import com.novafusion.net.AMFPHPConnection;

var connection:AMFPHPConnection = new AMFPHPConnection("http://localhost/amfphp/gateway.php", "UsingAMFPHP.TalkBack", "FlashPHPTest");

function result(event:Event):void {
trace(connection.result);
}

function fault(event:Event):void {
trace(connection.fault);
}

connection.setListeners(result, fault);
connection.talk("$1.put_together", "tom", "bob");
connection.talk("$2.spitBack", "tom");
Share on FacebookShare on Google+Share on StumbleUponTweet about this on TwitterEmail this to someone

Facebook comments:

2 thoughts on “Using the AMFPHPConnection class”

  1. Glad you like it. Hope you find the videos to be useful. By the way, if you wanted more to look at, I have about 90 video tutorials on there at the moment. But then again, most of them are probably too basic for you, lol.

Leave a Reply