This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Remoting Basics
|
409
Remoting Basics
All of the processing and conversion of AMF is handled internally by the FlashCom
Server (or Flash Player) and the Flash Remoting gateway, so you never have to deal
with it. The NetConnection class, the base class for RTMP communications, also
serves as the foundation for Remoting. A NetConnection instance on the client pro-
vides the developer interface to create the binary data and transmit it to the destina-
tion server.
Let’s start with a simple example in which the Flash Player is the client. You could
also run this example script inside a FlashCom application if the path to the imagi-
nary service were correct. Example 11-1 shows how to use a NetConnection object to
call a remote method on an application server with the remoting gateway installed.
The first two lines should look familiar to anyone building a FlashCom connection.
In this case, the URI passed to the connect( ) method refers to the remoting gateway
that comes bundled with ColdFusion residing on the local host. Unlike an RTMP
connection, the call to connect( ) does not actually make a connection to the server,
so the call will always return
true. Internally, the client (in this case, the Flash Player)
checks the protocol used for the connection. If the protocol is
rtmp://, rtmpt://,or
rtmps://, the Flash Player immediately tries to establish a socket connection with the
remote host, and the connect( ) method returns a Boolean value indicating whether
the URI is valid. If the protocol is unspecified or is anything other than
rtmp, rtmpt,
or
rtmps, the Flash Player doesn’t try to make a connection to the remote host and
always returns
true.
The next segment of code creates a response object (also known as a responder),
named
response, with an onResult( ) method to handle the response to the remote
method call. When the server sends back a response, the Player automatically calls
the onResult( ) method of the specified object and passes the results as a single argu-
ment. Within this body of this method is where you would either process the result
or forward the result to another object for processing.
The final line uses NetConnection.call( ) to invoke the remote procedure, using the
following format:
NetConnection.call(remoteMethod, resultObject | null [, p1, ...pn])
Example 11-1. Flash Remoting with NetConnection
my_conn = new NetConnection( );
my_conn.connect("http://localhost/flashservices/gateway");
response = new Object( );
response.onResult = function (result) {
trace(result);
};
my_conn.call("services.TestService.dataEcho", response, "test message");

Get Programming Flash Communication Server now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.