|
Flash Media Server Resources |
Client classContents [Hide]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();
};
Property summary
Method summary
Client.agentclientObject.agent Read-only; the version and platform of the client. When a client connects to the server, the format of Client.agent is as follows: Operating_System Flash_Player_Version For example, if Flash Player version 9.0.45.0 is running on Windows, the value of Client.agent is: "WIN 9,0,45,0". When a connection is made to another Flash Media Interactive Server, the format of Client.agent is as follows: Server_Name/Server_Version Operating_System/Operating_System_Build For example, if the server version is 3.0.0 and it’s running on Windows Server 2003, the value of Client.agent is: "FlashCom/3.0.0 WIN/5.1.2600". ExampleThe following example checks the agent property against the string "WIN" and executes different code depending on whether they match. This code is written in an onConnect() function: function onConnect(newClient, name){
if (newClient.agent.indexOf("WIN") > -1){
trace ("Window user");
} else {
trace ("non Window user.agent is" + newClient.agent);
}
}
Client.audioSampleAccessclientObject.audioSampleAccess Enables Flash Player to access raw, uncompressed audio data from streams in the specified folders. Call the SoundMixer.computeSpectrum() method in client-side ActionScript 3.0 to read the raw sound data for a waveform that is currently playing. For more information, see the SoundMixer.computeSpectrum() entry in the ActionScript 3.0 Language and Components Reference and “Accessing raw sound data” in Programming ActionScript 3.0. ExampleThe following server-side code sets the audioSampleAccess directory to publicdomain: application.onConnect = function(client) {
// Anyone can play free content, which is all streams placed under the
// samples/, publicdomain/ and contrib/ folders.
client.readAccess = "samples;publicdomain;contrib";
// Paying customers get to watch more streams.
if ( isPayingCustomer(client))
client.readAccess += "nonfree;premium";
// Content can be saved (user recorded streams) to contrib/ folder.
client.writeAccess = "contrib";
// Anyone can gain access to an audio snapshot of the publicdomain/ folder.
client.audioSampleAccess = "publicdomain";
// Paying customers can also get a video snapshot of the publicdomain/ folder.
if (isPayingCustomer(client))
client.videoSampleAccess = "publicdomain";
}
Client.call()clientObject.call(methodName, [resultObj, [p1, ..., pN]]) Executes a method in client-side code or on another server. The remote method can return data to the resultObj parameter, if provided. Whether the remote agent is a client or another server, the method is called on the remote agent’s NetConnection object. Parameters
ReturnsA boolean value of true if a call to methodName was successful on the client; otherwise, false. ExampleThe following example shows a client-side script that defines a function called getNumber(), which generates a random number: nc = new NetConnection();
nc.getNumber = function(){
return (Math.random());
};
nc.connect("rtmp:/clientCall");
The following server-side script calls Client.call() in the application.onConnect() handler to call the getNumber() method that was defined on the client. The server-side script also defines a function called randHander(), which is used in the Client.call() method as the resultObj parameter. randHandler = function(){
this.onResult = function(res){
trace("Random number: " + res);
}
this.onStatus = function(info){
trace("Failed with code:" + info.code);
}
};
application.onConnect = function(clientObj){
trace("Connected");
application.acceptConnection(clientObj);
clientObj.call("getNumber", new randHandler());
};
Note: This example does not work with
version 2 components. For an example of calling Client.call() when
using version 2 components, see application.onConnectAccept().
Client.checkBandwidth()clientObject.checkBandwidth() Call this method from a client-side script to detect client bandwidth. If the client is connected directly to the origin server, bandwidth detection occurs on the origin. If the client is connected to the origin server through an edge server, bandwidth detection happens at the first edge to which the client connected. To use this method to detect client bandwidth, define onBWDone() and onBWCheck() methods in a client-side script. For more information, see the Adobe Flash Media Server Developer Guide. Note: If you define the checkBandwidth() function
in a server-side script, the client call runs your definition instead
of the definition in the core server code.
Client.getBandwidthLimit()clientObject.getBandwidthLimit(iDirection) Returns the maximum bandwidth that the client or the server can use for this connection. Use the iDirection parameter to get the value for each direction of the connection. The value returned indicates bytes per second and can be changed with the Client.setBandwidthLimit() method. Set the default value for a connection in the Application.xml file of each application. You can call this method from a client-side script. Call the NetConnection.call() method
and pass it the name of the method, a result object, and any arguments,
as in the following:
var re:Responder = new Responder(res);
function res(info) {
trace(info);
for (var i:String in info) { trace(i + " - " + info[i]); }
}
nc.call("getBandwidthLimit", re, 0);
Client.getStats()clientObject.getStats() Returns statistics for the client. You can call this method from a client-side script. Call the NetConnection.call() method
and pass it the name of the method, a result object, and any arguments,
as in the following:
var re:Responder = new Responder(res);
function res(info) {
trace(info);
for (var i:String in info) { trace(i + " - " + info[i]); }
}
nc.call("getStats", re);
ReturnsAn Object with various properties for each statistic returned. The following table describes the properties of the returned object:
Client.ipclientObject.ip Read-only; A string containing the IP address of the client. ExampleThe following example uses the Client.ip property to verify whether a new client has a specific IP address. The result determines which block of code runs. application.onConnect = function(newClient, name){
if (newClient.ip == "127.0.0.1"){
// Insert code here.
} else {
// Insert code here.
}
};
Client.pageUrlclientObject.pageUrl Read-only; A string containing the URL of the web page in which the client SWF file is embedded. If the SWF file isn’t embedded in a web page, the value is the location of the SWF file. The following code shows the two examples: // trace.swf file is embedded in trace.html. client.pageUrl: http://www.example.com/trace.html // trace.swf is not embedded in an html file. client.pageUrl: http://www.example.com/trace.swf The value cannot be a local file address. ExampleThe following example uses the Client.pageUrl property to verify whether a new client is located at a particular URL. The result determines which block of code runs. application.onConnect = function(newClient){
if (newClient.pageUrl == "http://www.example.com/index.html"){
return true;
} else {
return false;
}
};
Client.ping()clientObject.ping() Sends a “ping” message to the client and waits for a response. If the client responds, the method returns true; otherwise, false. Use this method to determine whether the client connection is still active. Client.protocolclientObject.protocol Read-only; A string indicating the protocol used by the client to connect to the server. This string can have one of the following values:
ExampleThe following example checks the connection protocol used by a client upon connection to the application: application.onConnect(clientObj){
if(clientObj.protocol == "rtmp") {
trace("Client connected over RTMP");
} else if(clientOjb.protocol == "rtmpt") {
trace("Client connected over RTMP tunneled over HTTP");
}
}
Client.readAccessclientObject.readAccess A string of directories containing application resources (shared objects and streams) to which the client has read access. To give a client read access to directories that contain application resources, list the directories in a string delimited by semicolons. DetailsBy default, all clients have full read access, and the readAccess property is set to slash (/). To give a client read access, specify a list of access levels (in URI format), delimited by semicolons. Any files or directories within a specified URI are also considered accessible. For example, if myMedia is specified as an access level, any files or directories in the myMedia directory are also accessible (for example, myMedia/mp3s). Similarly, any files or directories in the myMedia/mp3s directory are also accessible, and so on. Clients with read access to a directory that contains streams can play streams in the specified access levels. Clients with read access to a directory that contains shared objects can subscribe to shared objects in the specified access levels and receive notification of changes in the shared objects.
Although you cannot use this property to control access for a particular file, you can create a separate directory for a file if you want to control access to it. Note: You cannot set this
property in the application.onPublish() event.
Client.referrerclientObject.referrer Read-only; A string containing the URL of the SWF file or the server in which this connection originated. The property is set when a SWF hosted on a web server or connects to an application on Flash Media Server. The property is also set when one Flash Media Server connects to another. This property is not set when a SWF from a local file system running in stand-alone Flash Player version 10 or above connects to Flash Media Server. If a SWF file is running in standalone Flash Player version 8 or 9, the property is set as file:///.... Client.remoteMethod()myClient.remoteMethod = function([p1, ..., pN]){}
You can define methods on the Client object and call the methods from client-side code. To call methods from client-side code, call the NetConnection.call() method and pass it the name of the method you defined. The server searches the Client object instance for the method. If the method is found, it is invoked and the return value is sent back to the result object specified in the call to NetConnection.call(). ExampleThe following example creates a method called sum() as a property of the Client object newClient on the server side: Client.prototype.sum = function(op1, op2){
return op1 + op2;
};
You can call the server-side sum() method from a client-side call to the NetConnection.call() method: nc = new NetConnection();
nc.connect("rtmp://myServer/myApp");
nc.call("sum", new result(), 20, 50);
function result(){
this.onResult = function (retVal){
output += "sum is " + retVal;
};
this.onStatus = function(errorVal){
output += errorVal.code + " error occurred";
};
}
You can also call the sum() method in server-side code: newClient.sum(); The following example creates two functions that you can call from either a client-side or server-side script: application.onConnect = function(clientObj) {
// The function foo returns 8.
clientObj.foo = function() {return 8;};
// The function bar is defined outside the onConnect call.
clientObj.bar = application.barFunction;
};
// The bar function adds the two values it is given.
application.barFunction = function(v1,v2) {
return (v1 + v2);
};
You can call either of the two functions that were defined in the previous example (foo and bar) by using the following code in a client-side script: c = new NetConnection();
c.call("foo");
c.call("bar", null, 1, 1);
You can call either of the two functions that were defined in the previous example (foo and bar) by using the following code in a server-side script: c = new NetConnection();
c.onStatus = function(info) {
if(info.code == "NetConnection.Connect.Success") {
c.call("foo");
c.call("bar", null, 2, 2);
}
};
Client.__resolve()Client.__resolve = function(propName){}
Provides values for undefined properties. When an undefined property of a Client object is referenced by Server-Side ActionScript code, the Client object is checked for a _resolve() method. If the object has a _resolve() method, it is invoked and passed the name of the undefined property. The return value of the _resolve() method is the value of the undefined property. In this way, _resolve() can supply the values for undefined properties and make it appear as if they are defined. Client.setBandwidthLimit()clientObject.setBandwidthLimit(iServerToClient, iClientToServer) Sets the maximum bandwidth for this client from client to server, server to client, or both. The default value for a connection is set for each application in the Client section of the Application.xml file. The value specified cannot exceed the bandwidth cap value specified in the Application.xml file. For more information, see BandwidthCap in the Adobe Flash Media Server Configuration and Administration Guide. You can call this method from a client-side script. Call the NetConnection.call() method
and pass it the name of the method, a result object, and any arguments,
as in the following:
var re:Responder = new Responder(res);
function res(info) {
trace(info);
for (var i:String in info) { trace(i + " - " + info[i]); }
}
nc.call("setBandwidthLimit", re, 125000, 125000);
Parameters
ExampleThe following example sets the bandwidth limits for each direction, based on values passed to the onConnect() function: application.onConnect = function(newClient, serverToClient, clientToServer){
newClient.setBandwidthLimit(serverToClient, clientToServer);
application.acceptConnection(newClient);
}
Client.uriclientObject.uri Read-only; the URI specified by the client to connect to this application instance. Client.videoSampleAccessclientObject.videoSampleAccess Enables Flash Player to access raw, uncompressed video data from streams in the specified folders. Call the BitmapData.draw() method in client-side ActionScript 3.0 to read the raw data for a stream that is currently playing. For more information, see the BitmapData.draw() entry in ActionScript 3.0 Language and Components Reference. ExampleThe following server-side code sets the videoSampleAccess directory to publicdomain for paying customers: application.onConnect = function(client) {
// Anyone can play free content, which is all streams placed under the
// samples/, publicdomain/, and contrib/ folders.
client.readAccess = "samples;publicdomain;contrib";
// Paying customers get to watch more streams.
if ( isPayingCustomer(client))
client.readAccess += "nonfree;premium";
// Content can be saved (user recorded streams) to the contrib/ folder.
client.writeAccess = "contrib";
// Anyone can gain access to an audio snapshot of the publicdomain/ folder.
client.audioSampleAccess = "publicdomain";
// Paying customers can also get a video snapshot of the publicdomain/ folder.
if (isPayingCustomer(client))
client.videoSampleAccess = "publicdomain";
}
Client.virtualKeyclientObject.virtualKey Use this property in conjunction with the Stream.setVirtualPath() method to map stream URLs to physical locations on the server. This allows you to serve different content to different versions of Flash Player. When a client connects, it receives a virtual key that corresponds to ranges that you set in the Vhost.xml file. You can use Client.virtualKey to change that value in a server-side script. The following is the code in the Vhost.xml file that you must configure: <VirtualKeys>
<!-- Create your own ranges and key values. -->
<!-- You can create as many Key elements as you need. -->
<Key from="WIN 7,0,19,0" to="WIN 9,0,0,0">A</Key>
</VirtualKeys>
Using the previous Vhost.xml file, if a Flash Player 8 client connected to the server, its Client.virtualKey value would be A. Note: A legal key cannot contain the characters “*”
and “:”.
Client.writeAccessclientObject.writeAccess Provides write access to directories that contain application resources (such as shared objects and streams) for this client. To give a client write access to directories that contain application resources, list directories in a string delimited by semicolons. By default, all clients have full write access, and the writeAccess property is set to slash (/). For example, if myMedia is specified as an access level, then any files or directories in the myMedia directory are also accessible (for example, myMedia/myStreams). Similarly, any files or subdirectories in the myMedia/myStreams directory are also accessible, and so on.
You cannot use this property to control access to a single file. To control access to a single file, create a separate directory for the file. Don’t precede the stream path with a leading slash (/) on the client side. Note: You cannot set this property in the application.onPublish() event.
ExampleThe following example provides write access to the /myMedia/myStreams and myData/notes directories: application.onConnect = function(newClient, name){
newClient.writeAccess = "/myMedia/myStreams;myData/notes";
application.acceptConnection();
};
The following example completely disables write access: application.onConnect = function(clientObj){
clientObj.writeAccess = "";
return true;
};
|