Server-Side Communication ActionScript > Application.registerClass |
![]() ![]() ![]() |
Application.registerClass
Availability
Flash Communication Server MX.
Usage
application.registerClass(className
,constructor
)
Parameters
className
The name of an ActionScript class.
constructor
A constructor function used to create an object of a specific class type during object deserialization. The name of the constructor function must be the same as className
. During object serialization, the name of the constructor function is serialized as the object's type. To unregister the class, pass the value null
as the constructor
parameter. Serialization is the process of turning an object into something you can send to another computer over the network.
Returns
Nothing.
Description
Method; registers a constructor function that is used when deserializing an object of a certain class type. If the constructor for a class is not registered, you cannot call the deserialized object's methods. This method is also used to unregister the constructor for a class. This is an advanced use of the server and is necessary only when sending ActionScript objects between a client and a server.
The client and the server communicate over a network connection. Therefore, if you use typed objects, each side must have the prototype of the same objects they both use. In other words, both the client-side and server-side ActionScript must define and declare the types of data they share so that there is a clear, reciprocal relationship between an object, method, or property on the client and the corresponding element on the server. You can use application.registerClass
to register the object's class type on the server side so that you can use the methods defined in the class.
Constructor functions should be used to initialize properties and methods; they should not be used for executing server code. Constructor functions are called automatically when messages are received from the client and need to be "safe" in case they are executed by a malicious client. You shouldn't define procedures that could result in negative situations such as filling up the hard disk or consuming the processor.
The constructor function is called before the object's properties are set. A class can define an onInitialize
method, which is called after the object has been initialized with all its properties. You can use this method to process data after an object is deserialized.
If you register a class that has its prototype set to another class, you must set the prototype constructor back to the original class after setting the prototype. The second example below illustrates this point.
Note: Client-side classes must be defined as function
function_name
(){}
, as shown in the following examples. If not defined in the correct way, application.registerClass will not properly identify the class when an instance of it is passed from the client to the server, and an error will be returned.
Example
This example defines a Color
constructor function with properties and methods. After the application connects, the registerClass
method is called to register a class for the objects of type Color
. When a typed object is sent from the client to the server, this class is called to create the server-side object. After the application stops, the registerClass
method is called again and passes the value null
to unregister the class.
function Color(){ this.red = 255; this.green = 0; this.blue = 0; } Color.prototype.getRed = function(){ return this.red; } Color.prototype.getGreen = function(){ return this.green; } Color.prototype.getBlue = function(){ return this.blue; } Color.prototype.setRed = function(value
){ this.red = value; } Color.prototype.setGreen = function(value
){ this.green = value; } Color.prototype.setBlue = function(value
){ this.blue = value; } application.onAppStart = function(){ application.registerClass("Color", Color); }; application.onAppStop = function(){ application.registerClass("Color", null); };
The following example shows how to use the application.registerClass
method with the prototype
property:
function A(){} function B(){} B.prototype = new A(); B.prototype.constructor = B; // set constructor back to that of B // insert code here application.registerClass("B", B);
![]() ![]() ![]() |