|
Flash Media Server Resources |
NetConnection classThe 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. Property summary
Method summary
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. Parameters
ExampleThe 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.
Parameters
ReturnsFor 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. ExampleThe 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.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:
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(). ParametersURI 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
NetConnection.isConnectednc.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. ExampleThe 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.objectEncodingnc.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:
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.
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. Parameters
ExampleThe 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);
}
}
};
|