Flash Media Server Developer Documentation

Client class

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 to call methods in client-side scripts.

When you call NetConnection.call() from a client-side ActionScript script, the method that is executed in the server-side script must be a method of the Client class. In your server-side script, you must define any method that you want to call from the client-side script. You can also call any methods that 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. 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, assign them to the prototype property of the Client class.

There are two steps to extending a built-in class by using the prototype property. 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 = "Adobe";

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

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

Availability

Flash Communication Server 1

Property summary

Property

Description

Client.agent

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

Client.audioSampleAccess

Enables Flash Player to access raw, uncompressed audio data from streams in the specified folders.

Client.id

Read-only; a string that uniquely identifies the client.

Client.ip

Read-only; A string containing the IP address of the client.

Client.pageUrl

Read-only; A string containing the URL of the web page in which the client SWF file is embedded.

Client.protocol

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

Client.readAccess

A string of directories containing application resources (shared objects and streams) to which the client has read access.

Client.referrer

Read-only; A string containing the URL of the SWF file or the server in which this connection originated.

Client.secure

Read-only; A boolean value that indicates whether this is an SSL connection (true) or not (false).

Client.uri

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

Client.videoSampleAccess

Enables Flash Player to access raw, uncompressed video data from streams in the specified folders.

Client.virtualKey

A virtual mapping for clients connecting to the server.

Client.writeAccess

Provides write access to directories that contain application resources (such as shared objects and streams) for this client.

Method summary

Method

Description

Client.call()

Executes a method on a client or on another server.

Client.checkBandwidth()

Call this method from a client-side script to detect bandwidth.

Client.getBandwidthLimit()

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

Client.getStats()

Returns statistics for the client.

Client.getStreamLength()

Returns the length of a stream, in seconds.

Client.ping()

Sends a "ping" message to the client and waits for a response.

Client.remoteMethod()

Invoked when a client or another server calls the NetConnection.call() method.

Client.__resolve()

Provides values for undefined properties.

Client.setBandwidthLimit()

Sets the maximum bandwidth for this client from client to server, server to client, or both.