hide side navigation
    5 most recent
    Web Services
    Library's
    Component's
    Applications
    Articles
  Flash button as Flex icon  Flex form by email  Hello Remoting with AS3  AS3 Saving data from Flash  AS3 Loading data into Flash  Fire Effect  Contact form  Dragable buttons  Hello World with openamf  Loading helper classes  Upload with Flash 8  Transitions effects  Snapshot with Flash 8  Hello World Remoting AS2  Flash AS2 Remoting Connector  Saving data from Flash  Loading data into Flash  FlashCom & Remoting login  Cell Renderer API  Editing a table using remoting components  Flash MX2004 web service classes  Browsing a catalog  amfphp Documentation  Hello World Remoting  Online Store with AMFPHP  Flash clients for Web Services  Web Service Walk Though with NuSoap  Popup windows in flash with javascript  Installing Apache/PHP  MoreOver News Feeds  Load Edit Save Text Files via CGI  Save Movie Clip Postion via PHP and MySQL
Current Page (4) << Previous Page 1 | 2 | 3 | 4

An Introduction to Flash Remoting

By: Jorge Solis

The Flash side

The flash movie needs to create a connection to the service, call a method, then wait for a response from the server. The following is the code that will be needed in the Flash movie to make this happen. The code should be placed in the first frame of the main timeline.

1. Create the connection


  #include "NetServices.as"
    #include "NetDebug.as"
    
NetServices.setDefaultGatewayUrl("http://localhost/Hello/gateway.php");
    
conn NetServices.createGatewayConnection(); 

In the first 2 lines we include two files that come with flash remoting and help to set up the connection and debug the result. The first line is a base library that helps to build the connection. The second line (NetDebug.as) is used by the debugger to show the connection status. You can open the NetConnection Debugger (menu Window, NetConnection Debugger, in Others Panels submenu under MX 2004) to see what happens when a request is made. After this we set the path to the default gateway for the application, this will be used for all of our services (for this connection). The last line is used to finally create the actual connection.

2. Create the response object


 
request 
= new Object(); //handles result
request.onResult = function(result){
    
show.text result;

In the above code we create an object to handle the results returned from the Remote Call. Each call to a remote procedure triggers an onResult event in the movie. Here's a table with the order in which result functions/methods are called:

Order Result function/method called
   
1 The methodName_result() method of the object passed to the remote procedure when called
2 The onResult() method of an object passed to the Remote Procedure when called
3 The methodName_Result() method of the default object passed to the getService() method
4 The onResult method of the default object passed to the getService() method
5 If no response object was found, then the results are displayed in the Output window (in test movie only)

In our example we're using the second one. If we want to use the first one, we need to use an echo_result function, and since we're not passing any object in the getService method as a second parameter, we can't use method 3 or 4. Our request object receive's a result parameter (if you take a look at our service definition, you can will see that it returns the same parameter that we pass it) and simply show the result in a textfield named show.

3. Call the service

We need to put a dynamic textfield on the stage with instance name of show and two simple buttons with instance name's of callButton and clearButton, respectively. Here's our final example:

 
service 
conn.getService("HelloWorld");
callButton.onPress = function(){
    
service.makeEcho(request"Hello World!");
}
clearButton.onPress = function(){
    
show.text "";
}

  

Lastly, we connect to the service (remember the file, class, and main function have the same name) and then we set the onPress event handler of  the callService button, where we call the makeEcho method. We pass a request object that we have defined earlier to handle the answer and a parameter, a string. The clear button will simply clear the textfield. This is not very useful, of course, but illustrates the method of calling remote procedures and let us be on the way.

Hello World Remoting !! Now you can get started with flash remoting with amfphp. We should have some new tutorials on this up soon.

- Jorge Solis

Current Page (4) << Previous Page 1 | 2 | 3 | 4