Flash Communication Server architecture overview > Application flow > Connection flow

 

Connection flow

When the client connects to the server, the server calls onAppStart if the application instance isn't already running. Next, the server-side onConnect method is invoked with a newly created Client object. The logic in this method determines whether to accept or reject the connection. Back on the client side, the onStatus method is called to report whether the connection was accepted or rejected. When the client closes the connection, the server-side onDisconnect method is called. When the application is unloaded, onAppStop is invoked.

Connection flow
 

Note: Only the client application can initiate a communication session. Both the client and the server send and receive status messages, open and close streams, save and share data, and end the network connection.

 
Invoking remote methods

After a successful connection, the client can invoke remote methods defined by the server component; the server application can also remotely invoke ActionScript methods defined in the Flash client movie.

 
Invoking server methods from the client

Each Flash movie that connects to a server application is represented by an instance of a server-side Client object. Each Client object instance, in turn, can have developer-defined methods associated with it that are accessible remotely. (You can also use the prototype property to create remote methods available to all clients. For an example of this, see the Client (object) entry in the Server-Side Communication ActionScript Dictionary.)

A Flash movie can then call the remotely-defined method using the NetConnection object's call method, and optionally specify a callback object to handle the result returned by the server. On the server, the method corresponding to the client call is invoked; and a result is returned to the client.

The following diagram illustrates an example of invoking a server-defined method from a client. In the diagram, the client invokes the remote method doThis over the NetConnection instance named nc. The doThis method is associated with an instance of the Client object, called clientObj in this case, that represents the client.

Remote method call flow from client to server, with result passed back to client
 

For more information on invoking server methods remotely, see the Client."commandName" entry in the Server-Side Communication ActionScript Dictionary.

 
Invoking client methods from the server

The process for invoking client-defined methods from the server is similar to the reverse case. On the client, you can attach a user-defined method to the NetConnection object instance used to connect to the server. That method can then be called remotely by the server using the Client object's call method.

The following diagram illustrates the process of invoking a client method from the server. In the diagram, the client-side method doThis is associated with an instance of the NetConnection object called nc. The server invokes the doThis method using the Client object's call method. The server-side script can optionally specify a result handler object. Again, the client can return a result, and the server's onResult handler is called on the callback object sent to the client.

Call flow from server to client, with result passed back to server
 

 
Shared object flow

Shared objects simplify the developer's task in sharing data between multiple users. A Flash client movie subscribes to a remote shared object by issuing a SharedObject.getRemote command, which returns a reference to the remote shared object. The client then connects the remote shared object to the NetConnection object by issuing a SharedObject.connect command.

Once the client is connected to the shared object, the server sends out a synchronization message for the shared object, which is handled by the SharedObject.onSync method handler defined on the client. When the client, the server, or any other movie instance makes a change to the shared object, the server again sends out a synchronization message for the shared object.

Each synchronization message contains a code that specifies the nature of the change that occurred to the shared object. The code changes depending on the circumstances. For example, when a client first connects to a shared object, the code that accompanies the synchronization message has a value of clear; a value of success indicates that the client successfully modified the contents of the shared object. For a complete list of codes and their descriptions, see the SharedObject.onSync entry in the Client-Side Communication ActionScript Dictionary.

The following diagram illustrates a scenario in which two clients, Client 1 and Client 2, connect to the same remote shared object, obj1.When each client first connects to the shared object, a clear synchronization message is returned by the server. Client 1 then modifies a data property of obj1, called x, setting it equal to "4". Client 1 receives a success synchronization message, indicating that the change to the shared object was successful; Client 2 receives a change synchronization message, indicating that the obj1 shared object has been modifed.

Shared object flow.