
Server-Side Communication ActionScript 31
The above 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
object 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
object, 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(){
// "writeAccess" property is already built-in to the client class
return this.writeAccess;
}
function Client_createUniqueID(){
var ipStr = this.ip;
// "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();
};
Commentaires sur ces manuels