Section 9.5. Server-to-Server Calls

9.5. Server-to-Server Calls

A powerful but often overlooked feature of FlashCom is the ability for a server-side script to create a NetConnection to another application instance on the same server or on any other machine running the FlashCom Server. This is a very important feature, which can be used to create lobby/rooms-style applications or to make applications scalable across multiple servers, as discussed in Chapter 16.

The server-to-server model closely mimics the client-to-server calls discussed in previous sections. In a server-side script, you can create a NetConnection object and connect it to a regular RTMP address, and the other application instance will treat the connecting instance just like any other client. One instance can connect to another one by creating a NetConnection object and then calling connect( ) :

 nc = new NetConnection(  ); nc.connect("rtmp://localhost/appName/instanceName"); 

The only way the server-side call differs from a client-side NetConnection.connect( ) call is that a full URI must be supplied, including the hostname. On many systems, "localhost" can be used to refer to the same server on which FlashCom is running. On others, "localhost" cannot be used, so the full hostname or IP address must be specified.

The instance being connected to receives a connection request from another instance, just like requests from Flash movies. The application.onConnect( ) method is called in the receiving application and passed a Client object that represents the connection request from the other application instance. Handling requests from both clients and instances can complicate the code within onConnect( ) methods . To differentiate between requests from Flash .swf files and those from other FlashCom instances, examine the ip , referrer , and agent properties of the Client object passed to onConnect( ) . For example:

 application.onConnect = function (client) {   if (client.ip == "127.0.0.1" &&  client.agent.substr(0,8) == "FlashCom"       &&  client.referrer.split("/")[3] == "lobby_rooms") {     // It is a   lobby_rooms   application instance     // running on the same server (127.0.0.1) trying to connect.     application.acceptConnection(client);   }   else {     // Otherwise, we assume the client represents a   .swf   .      client.writeAccess = "public";      client.readAccess = "public";     application.acceptConnection(client);   } }; 

The following section describes a full-blown example that uses client-to-server, server-to-client, and server-to-server calls to create a powerful lobby/rooms communication system. If you understand every line of code in the example, you have mastered the fundamentals of RMI in FlashCom.



Programming Flash Communication Server
Programming Flash Communication Server
ISBN: 0596005040
EAN: 2147483647
Year: 2003
Pages: 203

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net