Client class

Availability

Flash Communication Server 1.

The Client class lets you handle each user, or client, connection to a Flash Media Server application instance. The server automatically creates a Client object when a user connects to an application; the object is destroyed when the user disconnects from the application. Users have unique Client objects for each application to which they are connected. Thousands of Client objects can be active at the same time.

You can use the properties of the Client class to determine the version, platform, and IP address of each client. You can also set individual read and write permissions to various application resources such as Stream objects and shared objects. Use the methods of the Client class to set bandwidth limits and call methods in client-side scripts.

When you call NetConnection.call() from a client-side ActionScript script, the method that executes in the server-side script must be a method of the Client class. In your server-side script, you must define any method you want to call from the client-side script. You can also call any methods you define in the server-side script directly from the Client class instance in the server-side script.

If all instances of the Client class (each client in an application) require the same methods or properties, you can add those methods and properties to the class itself instead of adding them to each instance of a class. This process is called extending a class. You can extend any server-side or client-side ActionScript class. To extend a class, instead of defining methods in the constructor function of the class or assigning them to individual instances of the class, you assign methods to the prototype property of the constructor function of the class. When you assign methods and properties to the prototype property, the methods are automatically available to all instances of the class.

The following code shows how to assign methods and properties to an instance of a class. In the application.onConnect handler, the client instance clientObj is passed to the server-side script as a parameter. You can then assign a property and method to the client instance:

application.onConnect = function(clientObj){
    clientObj.birthday = myBDay; 
    clientObj.calculateDaysUntilBirthday = function(){
        // Insert code here.
    }
};

The previous example works, but must be executed every time a client connects. If you want the same methods and properties to be available to all clients in the application.clients array without defining them every time, you must assign them to the prototype property of the Client class. There are two steps to extending a built-in class using the prototype method. You can write the steps in any order in your script. The following example extends the built-in Client class, so the first step is to write the function that you will assign to the prototype property:

// First step: write the functions.

function Client_getWritePermission(){
// The writeAccess property is already built in to the client class.
    return this.writeAccess;
}

function Client_createUniqueID(){
    var ipStr = this.ip;    
// The ip property is already built in to the client class.
    var uniqueID = "re123mn"
// You would need to write code in the above line
// that creates a unique ID for each client instance.
    return uniqueID;
}

// Second step: assign prototype methods to the functions.

Client.prototype.getWritePermission = Client_getWritePermission;
Client.prototype.createUniqueID = Client_createUniqueID;

// A good naming convention is to start all class method 
// names with the name of the class followed by an underscore.

You can also add properties to prototype, as shown in the following example:

Client.prototype.company = "Macromedia";

The methods are available to any instance, so within application.onConnect, which is passed a clientObj argument, you can write the following code:

application.onConnect = function(clientObj){
    var clientID = clientObj.createUniqueID();
    var clientWritePerm = clientObj.getWritePermission();
};

Method summary for the Client class

Method

Description

Client.call()

Executes a method on the Flash client asynchronously and returns the value from the Flash client to the server.

Client.getBandwidthLimit()

Returns the maximum bandwidth the client or the server can attempt to use for this connection.

Client.getStats()

Returns statistics for the client.

Client.readAccess

Sends a "ping" message to the client. If the client responds, the method returns true; otherwise it returns false.

Client.__resolve

Provides values for undefined properties.

Client.setBandwidthLimit()

Sets the maximum bandwidth for the connection.

Property summary for the Client class

Property

Description

Client.agent

Read-only; the version and platform of the Flash client.

Client.ip

Read-only; the IP address of the Flash client.

Client.protocol

Read-only; the protocol used by the client to connect to the server.

Client.readAccess

A list of access levels to which the client has read access.

Client.referrer

Read-only; the URL of the SWF file or server where this connection originated.

Client.secure

Read-only; a Boolean value indicating whether an Internet connection is secure (true) or not (false).

Client.uri

Read-only; the URI specified by the client to connect to this application instance.

Client.virtualKey

The user agent type of the client (which is typically the Flash Player version), but may be set to any legal key value.

Client.writeAccess

A list of access levels to which the client has write access.

Event handler summary for the Client class

Event handler

Description

Client."commandName"

Invoked when NetConnection.call(commandName) is called in a client-side script.


 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/fms/2/docs/00000666.html