NetConnection Class

NetConnection ClassFlash 6

enables a connection to a remote server
myNetConnectionObject.methodName(params)

Methods

addHeader( )

Adds a header to every AMF packet in this connection.

call( )

Invokes a service method on the remote server.

clone( )

Creates a clone of a NetConnection object, without headers.

close( )

Closes the NetConnection object.

connect( )

Defines the URL used in a Flash Remoting connection.

getDebugConfig( )

Retrieves a NetDebugConfig object with all subscribed events.

getDebugId( )

Retrieves the NetConnection object's debug identifier.

getService( )

Creates a service object that acts as a proxy to a remote service method.

ReplaceGatewayUrl( )

Changes the gateway URL for the current connection.

RequestPersistentHeader( )

A method that can be initiated by a server-side service to add a header to the request packets.

setCredentials( )

Creates a credentials header that is attached to each AMF packet.

setDebugId( )

Creates a user-defined debug identifier.

trace( )

Sends output to the NetConnection Debugger panel.

Only the public methods of the NetConnection class are shown. Many of the methods require the inclusion of the NetDebug.as or NetServices.as file, as indicated in the footnotes.

Description

The NetConnection class is at the heart of Flash Remoting. The class is a core part of Flash MX after the installation of the Flash Remoting components; however, the NetServices.as and NetDebug.as files add several methods to the NetConnection class, as noted under the Methods heading.

A NetConnection object allows communication between the Flash movie and Flash Remoting on the server-side service. Methods of the NetConnection class allow the developer to call remote services and process the results within the Flash movie. A NetConnection object is typically created in the initialization phase of the Flash movie in the first frame on the main timeline (following any preloader) by calling NetServices.createGatewayConnection( ).

An important method of the NetConnection class is the getService( ) method. The call to NetConnection.getService( ) returns a service object (of the NetServiceProxy class), which acts as proxy to the remote service. Developers can invoke remote service functions as methods of the service object.

Examples

There are two ways of creating NetConnection objects in your Flash Remoting application. The first was demonstrated in most of the examples in this book. It involves the use of the NetServices.as file and the NetServices.createGatewayConnection( ) method:

#include "NetServices.as"
if (connected == null) {
  // Initialize the variable, so this section is only called once
  connected = true;
  // Set the default URL, so the NetConnection object knows how to connect
  NetServices.setDefaultGatewayUrl("http://127.0.0.1/flashservices/gateway");
  // Create the NetConnection object
  var my_conn = NetServices.createGatewayConnection( );
}

A variation of this approach is to omit the call to setDefaultGatewayUrl( ) and pass the gateway URL as a parameter to createGatewayConnection( ) instead. See those methods under the NetServices class in this chapter for more information.

Including the NetServices.as file also adds other methods to the NetConnection class, such as getService( ) and setCredentials( ).

The second way of creating a NetConnection object is to simply use the new keyword:

if (connected == null) {
  // Initialize the variable so this section is only called once
  connected = true;
  // Create the NetConnection object
  var my_conn = new NetConnection( );
  // Set the URL to the NetConnection object
  my_conn.connect("http://127.0.0.1/flashservices/gateway");
}

Using this technique, it is not necessary to include NetServices.as, but if you don't include it you will not have access to the methods of the NetConnection class contained in NetServices.as.

There are several private methods of the NetConnection class that are included when you include the NetDebug.as file in your Flash movie, in addition to those noted under the Methods heading. The NetConnection Debugger panel uses these methods internally: attachDebug( ), sendDebugEvent( ), sendServerEvent( ), sendClientEvent( ), addNetDebugHeader( ), updateConfig( ), isRealTime( ), setupRecordset( ), as well as several methods that take the place of actual NetConnection methods.

See Also

NetConnection.call( ), NetConnection.connect( ), the NetDebug class, the NetDebugConfig class, NetServices.createGatewayConnection( ), NetServices.setDefaultGate-wayUrl( ); Chapter 4; the NetServices.as, NetDebug.as, NetConnection.as, and RecordSet.as files in your Flash Includes directory



    Part III: Advanced Flash Remoting
     
    ASPTreeView.com
     
    Evaluation has ГР·ДУexpired.
    Info...