AS3 Flash Remoting Example

Using remoting in AS3 is actually quite simple. Remoting calls are now made directly through NetConnection, so there’s no need for Remoting classes or components, everything you need is readily available. Here is a quick example.

This is a wrapper class for NetConnection that makes sure we are using the right AMF encoding. The default is AMF3, which is not supported on AMFPHP versions lower than 2.0. So, we use AMF0 instead:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package
{
	import flash.net.NetConnection;
	import flash.net.ObjectEncoding;
 
	public class RemotingService extends NetConnection
	{		
		function RemotingService(url:String)
		{
			// Set AMF version for AMFPHP
			objectEncoding = ObjectEncoding.AMF0;
			// Connect to gateway
			connect(url);
		}
	}
}

The next class is just a test that uses the previous class to make a call.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package 
{
	import flash.net.Responder;
 
	public class RemotingTest
	{
		private var rs:RemotingService;
 
		function RemotingTest()
		{
			init();
		}
 
		private function init()
		{
			rs = new RemotingService("http://your.domain.com/amfphp/gateway.php");
			var responder:Responder = new Responder(onResult, onFault);
			var params:Object = new Object();
			params.arg1 = "something";
			params.arg2 = "2";
			rs.call("Class.method", responder, params);
		}
 
		private function onResult(result:Object):void
		{
			trace(result);
		}
 
		private function onFault(fault:Object):void
		{
			trace(fault);
		}
	}
}

Given the following PHP class, the previous exercise could be easily adjusted to print “something and 2″ to the Output panel. Just copy it to the ‘services’ folder of your AMFPHP installation, and replace “Class.method” with “Test.test” on the RemotingTest class.

Test.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
 
class Test
{
 
	function __construct()
	{
		$this->methodTable = array(
 
				"test" => array(
				"description" => "Tests service",
				"access" => "remote"
			)
		);
	}
 
 
	function test($params)
	{
		return $params['arg1'] . " and " . $params['arg2'];
	}
 
}
 
?>

March 12, 2007

Posted by: Oscar Trelles

Category: Uncategorized

Tags:

Comments (2)

Cheyennemtnman

September 5th, 2010 at 2:09 pm    


Okay but how can I use this in a FLash CS5 form submitting data to and from a MySql database?

valetine

July 18th, 2011 at 6:19 am    


what about the flash cs5 and coldfusion 9 version

Leave a reply

Name *

Mail *

Website