NetConnection class



The server-side NetConnection class lets you create a two-way connection between a Flash Media Server application instance and an application server, another Flash Media Server, or another Flash Media Server application instance on the same server. You can use NetConnection objects to create powerful applications; for example, you can get weather information from an application server or share an application load with other servers that are running Flash Media Server or application instances.

Availability

Flash Communication Server 1

Property summary

Property

Description

NetConnection.isConnected

Read-only; a boolean value indicating whether a connection has been made.

NetConnection.objectEncoding

The Action Message Format (AMF) version used to pass binary data between two servers.

NetConnection.uri

Read-only; a string indicating the URI parameter of the NetConnection.connect() method.

Method summary

Method

Description

NetConnection.addHeader()

Adds a context header to the Action Message Format (AMF) packet structure.

NetConnection.call()

Invokes a command or method on another Flash Media Server or an application server to which the application instance is connected.

NetConnection.close()

Closes the connection with the server.

NetConnection.connect()

Connects to another Flash Media Server or to a Flash Remoting server such as Adobe ColdFusion.

Event handler summary

Event handler

Description

NetConnection.onStatus()

Invoked every time the status of the NetConnection object changes.

NetConnection constructor

new NetConnection()

Creates a new instance of the NetConnection class.

Availability

Flash Communication Server 1.

Returns

A NetConnection object.

Example

The following example creates a new instance of the NetConnection class:

newNC = new NetConnection();

NetConnection.addHeader()

nc.addHeader(name, mustUnderstand, object)

Adds a context header to the Action Message Format (AMF) packet structure. This header is sent with every future AMF packet. If you call addHeader() by using the same name, the new header replaces the existing header, and the new header persists for the duration of the NetConnection object. To remove a header, call addHeader() and pass it the name of the header to remove and an undefined object.

Availability

Flash Communication Server 1

Parameters

name
A string; identifies the header and the ActionScript object data associated with it.

mustUnderstand
A boolean; true indicates that the server must understand and process this header before it handles any of the following headers or messages.

object
An Object.

Example

The following example creates a new NetConnection instance, nc, and connects to an application at web server www.foo.com that is listening at port 1929. This application dispatches the service /blag/SomeCoolService. The last line of code adds a header called foo.

nc=new NetConnection(); 
nc.connect("http://www.foo.com:1929/blag/SomeCoolService"); 
nc.addHeader("foo", true, new Foo());

NetConnection.call()

nc.call(methodName, [resultObj [, p1, ..., pN])

Invokes a command or method on another Flash Media Server or an application server to which the application instance is connected. The NetConnection.call() method on the server works the same way as the NetConnection.call() method on the client: it invokes a command on a remote server.

Note: To call a method on a client from a server, use the Client.call() method.

Availability

Flash Communication Server 1

Parameters

methodName
A string indicating a method specified in the form "[objectPath/]method". For example, the someObj/doSomething command tells the remote server to invoke the clientObj.someObj.doSomething() method, with all the p1, ..., pN parameters. If the object path is missing, clientObj.doSomething() is invoked on the remote server.

resultObj
An Object. This optional parameter is used to handle return values from the server. The result object can be any object that you defined and can have two defined methods to handle the returned result: onResult() and onStatus(). If an error is returned as the result, onStatus() is invoked; otherwise, onResult() is invoked.

p1, ..., pN
Optional parameters that can be of any ActionScript type, including a reference to another ActionScript object. These parameters are passed to the methodName parameter when the method is executed on the remote application server.

Returns

For RTMP connections, returns a boolean value of true if a call to methodName is sent to the client; otherwise, false. For application server connections, it always returns true.

Example

The following example uses RTMP to execute a call from one Flash Media Server to another Flash Media Server. The code makes a connection to the App1 application on server 2 and then invokes the Sum() method on server 2:

nc1.connect("rtmp://server2.mydomain.com/App1", "svr2",); 
nc1.call("Sum", new Result(), 3, 6);

The following Server-Side ActionScript code is on server 2. When the client is connecting, this code checks to see whether it has a parameter that is equal to svr1. If the client has that parameter, the Sum() method is defined so that when the method is called from svr1, svr2 can respond with the appropriate method:

application.onConnect = function(clientObj){ 
    if(arg1 == "svr1"){ 
        clientObj.Sum = function(p1, p2){ 
            return p1 + p2; 
        } 
    }  
    return true;  
};

The following example uses an Action Message Format (AMF) request to make a call to an application server. This allows Flash Media Server to connect to an application server and then invoke the quote() method. The Java™ adaptor dispatches the call by using the identifier to the left of the dot as the class name and the identifier to the right of the dot as a method of the class.

nc = new NetConnection; 
nc.connect("http://www.xyz.com/java"); 
nc.call("myPackage.quote", new Result());

NetConnection.close()

nc.close()

Closes the connection with the server. After you close the connection, you can reuse the NetConnection instance and reconnect to an old application or connect to a new one.

Note: The NetConnection.close() method has no effect on HTTP connections.

Availability

Flash Communication Server 1

NetConnection.connect()

nc.connect(URI, [p1, ..., pN])

Connects to another Flash Media Server or to a Flash Remoting server such as Adobe® ColdFusion®.

Call NetConnection.connect() to connect over HTTP to an application server running a Flash Remoting gateway. Call NetConnection.connect() to connect to another Flash Media Server for sharing audio, video, and data over one of the following versions of RTMP:

Protocol

Description

RTMP

Real-Time Messaging Protocol

RTMPS

Real-Time Messaging Protocol over SSL

Note: You can use HTTP to connect to a Flash Remoting gateway only. You cannot use HTTP to connect to another Flash Media Server or to media assets.

It is good practice to write an application.onStatus() callback function and check the NetConnection.isConnected property for RTMP connections to see whether a successful connection was made. For Action Message Format (AMF) connections, check NetConnection.onStatus().

Availability

Flash Communication Server 1

Parameters

URI  A string indicating a URI to connect to. URI has the following format:

[protocol://]host[:port]/appName[/instanceName]

The following are legal URIs:

http://appServer.mydomain.com/webApp 
rtmp://rtserver.mydomain.com/realtimeApp 
rtmps://rtserver.mydomain.com/secureApp 
rtmp://localhost/realtimeApp 
rtmp:/realtimeApp
p1, ..., pN
Optional parameters that can be of any ActionScript type, including references to other ActionScript objects. These parameters are sent as connection parameters to the application.onConnect() event handler for RTMP connections. For AMF connections to application servers, RTMP parameters are ignored.

Returns

For RTMP connections, a boolean value of true for success; otherwise, false. For AMF connections to application servers, true is always returned.

Example

The following example creates an RTMP connection to an application instance on Flash Media Server:

nc = new NetConnection(); 
nc.connect("rtmp://tc.foo.com/myApp/myConn");

NetConnection.isConnected

nc.isConnected

Read-only; a boolean value indicating whether a connection has been made. It is set to true if there is a connection to the server. It’s a good idea to check this property value in an onStatus() callback function. This property is always true for AMF connections to application servers.

Availability

Flash Communication Server 1

Example

The following example uses NetConnection.isConnected in an onStatus() handler to check whether a connection has been made:

nc = new NetConnection(); 
nc.connect("rtmp://tc.foo.com/myApp"); 
nc.onStatus = function(infoObj){ 
    if (info.code == "NetConnection.Connect.Success" && nc.isConnected){ 
        trace("We are connected"); 
    } 
};

NetConnection.objectEncoding

nc.objectEncoding

The Action Message Format (AMF) version used to pass binary data between two servers. The possible values are 3 (ActionScript 3.0 format) and 0 (ActionScript 1.0 and ActionScript 2.0 format). The default value is 3. When Flash Media Server acts as a client trying to connect to another server, the encoding of the client should match the encoding of the remote server.

The value of objectEncoding is determined dynamically according to the following rules when the server receives a NetConnection.onStatus() event with the code property NetConnection.Connect.Success:

  • If the onStatus()info object contains an objectEncoding property, its value is used.

  • If the onStatus()info object does not contain an objectEncoding property, 0 is assumed even if the connecting server has set objectEncoding to 3.

  • Once the NetConnection instance is connected, the objectEncoding property is read-only.

These rules turn Flash Media Server 3 into an AMF0 client when it connects to a remote Flash Media Server version 2 or earlier (which only support AMF0).

Note: The server always serializes data in AMF0 while executing Flash Remoting functions.

Availability

Flash Media Interactive Server 3 and Flash Media Development Server 3

NetConnection.onStatus()

nc.onStatus = function(infoObject) {}

Invoked every time the status of the NetConnection object changes. For example, if the connection with the server is lost in an RTMP connection, the NetConnection.isConnected property is set to false, and NetConnection.onStatus() is invoked with a status message of NetConnection.Connect.Closed. For AMF connections, NetConnection.onStatus() is used only to indicate a failed connection. Use this event handler to check for connectivity.

Availability

Flash Communication Server 1

Parameters

infoObject
An Object with properties that provide information about the status of a NetConnection information object. This parameter is optional, but it is usually used. The NetConnection information object contains the following properties:

Property

Meaning

code

A string identifying the event that occurred.

description

A string containing detailed information about the code. Not every information object includes this property.

level

A string indicating the severity of the event.

The following table contains the code and level property values and their meanings:

Code

Level

Meaning

NetConnection.Call.Failed

error

The NetConnection.call() method was not able to invoke the server-side method or command.

NetConnection.Connect.AppShutdown

error

The application has been shut down (for example, if the application is out of memory resources and must shut down to prevent the server from crashing) or the server has shut down.

NetConnection.Connect.Closed

status

The connection was closed successfully.

NetConnection.Connect.Failed

error

The connection attempt failed.

NetConnection.Connect.Rejected

error

The client does not have permission to connect to the application, or the application name specified during the connection attempt was not found on the server. This information object also has an application property that contains the value returned by application.rejectConnection().

NetConnection.Connect.Success

status

The connection attempt succeeded.

NetConnection.Proxy.NotResponding

error

The proxy server is not responding. See the ProxyStream class.

Example

The following example defines a function for the onStatus() handler that outputs messages to indicate whether the connection was successful:

nc = new NetConnection(); 
nc.onStatus = function(info){ 
    if (info.code == "NetConnection.Connect.Success") { 
        _root.gotoAndStop(2); 
    }    else { 
        if (! nc.isConnected){ 
            _root.gotoAndStop(1); 
        } 
    } 
};

NetConnection.uri

nc.uri

Read-only; a string indicating the URI parameter of the NetConnection.connect() method. This property is set to null before a call to NetConnection.connect() or after a call to NetConnection.close().

Availability

Flash Communication Server 1