PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Connecting to the Server from Flash

Connecting to your Zend_Amf_Server from your Flash project is slightly different than from Flex. However once the connection Flash functions with Zend_Amf_Server the same way is flex. The following example can also be used from a Flex AS3 file. We will reuse the same Zend_Amf_Server configuration along with the World class for our connection.

Open Flash CS and create and new Flash File (ActionScript 3). Name the document ZendExample.fla and save the document into a folder that you will use for this example. Create a new AS3 file in the same directory and call the file Main.as. Have both files open in your editor. We are now going to connect the two files via the document class. Select ZendExample and click on the stage. From the stage properties panel change the Document class to Main. This links the Main.as ActionScript file with the user interface in ZendExample.fla. When you run the Flash file ZendExample the Main.as class will now be run. Next we will add ActionScript to make the AMF call.

We now are going to make a Main class so that we can send the data to the server and display the result. Copy the following code into your Main.as file and then we will walk through the code to describe what each element's role is.

package {
  import flash.display.MovieClip;
  import flash.events.*;
  import flash.net.NetConnection;
  import flash.net.Responder;

  public class Main extends MovieClip {
    private var gateway:String = "http://example.com/server.php";
    private var connection:NetConnection;
    private var responder:Responder;

    public function Main() {
      responder = new Responder(onResult, onFault);
      connection = new NetConnection;
      connection.connect(gateway);
    }

    public function onComplete( e:Event ):void{
      var params = "Sent to Server";
      connection.call("World.hello", responder, params);
    }

    private function onResult(result:Object):void {
      // Display the returned data
      trace(String(result));
    }
    private function onFault(fault:Object):void {
      trace(String(fault.description));
    }
  }
}

We first need to import two ActionScript libraries that perform the bulk of the work. The first is NetConnection which acts like a by directional pipe between the client and the server. The second is a Responder object which handles the return values from the server related to the success or failure of the call.

import flash.net.NetConnection;
import flash.net.Responder;

In the class we need three variables to represent the NetConnection, Responder, and the gateway URL to our Zend_Amf_Server installation.

private var gateway:String = "http://example.com/server.php";
private var connection:NetConnection;
private var responder:Responder;

In the Main constructor we create a responder and a new connection to the Zend_Amf_Server endpoint. The responder defines two different methods for handling the response from the server. For simplicity I have called these onResult and onFault.

responder = new Responder(onResult, onFault);
connection = new NetConnection;
connection.connect(gateway);

In the onComplete function which is run as soon as the construct has completed we send the data to the server. We need to add one more line that makes a call to the Zend_Amf_Server World->hello function.

connection.call("World.hello", responder, params);

When we created the responder variable we defined an onResult and onFault function to handle the response from the server. We added this function for the successful result from the server. A successful event handler is run every time the connection is handled properly to the server.

private function onResult(result:Object):void {
    // Display the returned data
    trace(String(result));
}

The onFault function, is called if there was an invalid response from the server. This happens when there is an error on the server, the URL to the server is invalid, the remote service or method does not exist, and any other connection related issues.

private function onFault(fault:Object):void {
    trace(String(fault.description));
}

Adding in the ActionScript to make the remoting connection is now complete. Running the ZendExample file now makes a connection to Zend_Amf. In review you have added the required variables to open a connection to the remote server, defined what methods should be used when your application receives a response from the server, and finally displayed the returned data to output via trace().

Zend Framework