Recipe 14.14 Implementing Server-Side ActionScript

14.14.1 Problem

You want to handle advanced FlashCom application needs with server-side functionality.

14.14.2 Solution

Place the server-side code in a main.asc or [registered_app_name].asc file in the FlashCom application's root directory.

14.14.3 Discussion

Server-Side ActionScript must be placed within .asc files in the FlashCom application directory. FlashCom always looks for an .asc file named main.asc or [registered_app_name].asc (such as myApplication.asc) in the application's root directory. FlashCom also detects files named main.js or [registered_app_name].js as alternatives to the files with .asc file extensions.

If none of these files exist, there is no custom server-side functionality. On the other hand, if FlashCom does find one of these files, it automatically looks for special application event handler methods when the corresponding events occur. Of these event handler methods, here are three of the most common:

application.onAppStart( )

This method is automatically called when the application first starts. You should place all your application initialization code within this method.

application.onConnect( )

This method is automatically called when a client connects to the application. FlashCom creates a new client object on the server and passes a reference to that object to the onConnect( ) method. Additionally, if the client-side connect( ) method includes any extra parameters, those values are also passed to onConnect( ). The onConnect( ) method is the place to do any client initialization, such as adding methods to the client object that can be invoked from the client-side movie. Also, you must either accept or reject connecting clients with the application.acceptConnection( ) or application.rejectConnection( ) methods.

application.onDisconnect( )

This method is automatically called when a client disconnects from the application. FlashCom passes to this method a reference to the client object corresponding to the user who has disconnected from the application. This is the place to clean up anything that needs to be taken care of when a client disconnects. For example, you might want to remove the client from the list of connected users in a chat room.

Here is an example of a main.asc file, which can be placed in the FlashCom application's directory. It defines the three most common handlers you'll use to detect FlashCom events.

application.onAppStart = function (  ) {

  // When the application starts up, create a server-side remote shared object and
  // save it to an application-wide property named mySsRso.
  application.mySsRso = SharedObject.get("mySharedObject");

  // Create an application-wide property to keep track of the total number of users
  // that have connected since the application started.
  application.usersConnectedFromAppStart = 0;

  // Create an application-wide property to keep track of the total number of users
  // that have disconnected since the application started.
  application.usersDisconnectedFromAppStart = 0;

application.onConnect = function (newClient) {
  // Accept the connection to the new client.

  // Increment the number of connected users.
  application.usersConnectedFromAppStart += 1;

application.onDisconnect = function (disconnectClient) {
  // Increment the number of disconnected users.
  application.usersDisconnectedFromAppStart += 1;

14.14.4 See Also

Recipe 14.16. For more information about Server-Side ActionScript for FlashCom, see:

    Part I: Local Recipes
    Part II: Remote Recipes