|
Flash Media Server Resources |
SharedObject classContents [Hide]The SharedObject class lets you store data on the server and share data between multiple client applications in real time. Shared objects can be temporary, or they can persist on the server after an application has closed; you can consider shared objects as real-time data transfer devices. Note: The following information explains the server-side
SharedObject class. You can also create shared objects with the
client-side SharedObject class.
The following list describes common ways to use shared objects in Server-Side ActionScript:
Property summary
Method summary
Event handler summary
SharedObject.autoCommitso.autoCommit A boolean value indicating whether the server periodically stores all persistent shared objects (true) or not (false). If autoCommit is false, the application must call SharedObject.commit() to save the shared object; otherwise, the data is lost. This property is true by default. To override the default, specify the initial state by using the following configuration key in the Application.xml file, as shown in the following example: <SharedObjManager>
<AutoCommit>false</AutoCommit>
</SharedObjManager>
SharedObject.clear()so.clear() Deletes all the properties of a single shared object and sends a clear event to all clients that subscribe to a persistent shared object. The persistent data object is also removed from a persistent shared object. SharedObject.close()so.close() Detaches a reference from a shared object. A call to the SharedObject.get() method returns a reference to a shared object instance. The reference is valid until the variable that holds the reference is no longer in use and the script is garbage collected. To destroy a reference immediately, you can call SharedObject.close(). You can use SharedObject.close() when you no longer want to proxy a shared object. SharedObject.commit()so.commit([name]) Static; stores either a specific persistent shared object instance or all persistent shared object instances with an isDirty property whose value is true. Use this method if the SharedObject.autoCommit property is false and you need to manage when a shared object is stored locally. SharedObject.flush()so.flush() Saves the current state of a persistent shared object. Invokes the SharedObject.onStatus() handler and passes it an object that contains information about the success or failure of the call. ExampleThe following example places a reference to the shared object foo in the variable so. It then locks the shared object instance so that no one can make any changes to it and saves the shared object by calling so.flush(). After the shared object is saved, it is unlocked so that further changes can be made. var so = SharedObject.get("foo", true);
so.lock();
// Insert code here that operates on the shared object.
so.flush();
so.unlock();
SharedObject.get()SharedObject.get(name, persistence [, netConnection]) Static; creates a shared object or returns a reference to an existing shared object. To perform any operation on a shared object, the server-side script must get a reference to the shared object by using the SharedObject.get() method. If the requested object is not found, a new instance is created. Parameters
DetailsThere are two types of shared objects, persistent and nonpersistent, and they have separate namespaces. This means that a persistent and a nonpersistent shared object can have the same name and exist as two distinct shared objects. Shared objects are scoped to the namespace of the application instance and are identified by a string. The shared object names should conform to the URI specification. You can also call SharedObject.get() to get a reference to a shared object that is in a namespace of another application instance. This instance can be on the same server or on a different server and is called a proxied shared object. To get a reference to a shared object from another instance, create a NetConnection object and use the NetConnection.connect() method to connect to the application instance that owns the shared object. Pass the NetConnection object as the netConnection parameter of the SharedObject.get() method. The server-side script must get a reference to a proxied shared object before there is a request for the shared object from any client. To do this, call SharedObject.get() in the application.onAppStart() handler. If you call SharedObject.get() with a netConnection parameter and the local application instance already has a shared object with the same name, the shared object is converted to a proxied shared object. All shared object messages for clients that are connected to a proxied shared object are sent to the master instance. If the connection state of the NetConnection object that was used as the netConnection parameter changes state from connected to disconnected, the proxied shared object is set to idle and any messages received from subscribers are discarded. The NetConnection.onStatus() handler is called when a connection is lost. You can then reestablish a connection to the remote instance and call SharedObject.get(), which changes the state of the proxied shared object from idle to connected. If you call SharedObject.get() with a new NetConnection object on a proxied shared object that is already connected, and if the URI of the new NetConnection object doesn’t match the current NetConnection object, the proxied shared object unsubscribes from the previous shared object, sends a clear event to all subscribers, and subscribes to the new shared object instance. When a subscribe operation to a proxied shared object is successful, all subscribers are reinitialized to the new state. This process lets you migrate a shared object from one application instance to another without disconnecting the clients. Updates received by proxied shared objects from subscribers are checked to see if the update can be rejected based on the current state of the proxied shared object version and the version of the subscriber. If the change can be rejected, the proxied shared object doesn’t forward the message to the remote instance; the reject message is sent to the subscriber. The corresponding client-side ActionScript method is SharedObject.getRemote(). ExampleThe following example creates a shared object named foo in the function onProcessCmd(). The function is passed a parameter, cmd, that is assigned to a property in the shared object. function onProcessCmd(cmd){
// Insert code here.
var shObj = SharedObject.get("foo", true);
propName = cmd.name;
shObj.getProperty (propName, cmd.newAddress);
}
The following example uses a proxied shared object. A proxied shared object resides on a server or in an application instance (called master) that is different from the server or application instance that the client connects to (called proxy). When the client connects to the proxy and gets a remote shared object, the proxy connects to the master and gives the client a reference to this shared object. The following code is in the main.asc file: application.appStart = function() {
nc = new NetConnection();
nc.connect("rtmp://" + master_server + "/" + master_instance);
proxySO = SharedObject.get("myProxy",true,nc);
// Now, whenever the client asks for a persistent
// shared object called myProxy, it receives themyProxy
// shared object from master_server/master_instance.
};
SharedObject.getProperty()so.getProperty(name) Retrieves the value of a named property in a shared object. The returned value is a copy associated with the property, and any changes made to the returned value do not update the shared object. To update a property, use the SharedObject.setProperty() method. SharedObject.getPropertyNames()so.getPropertyNames() Enumerates all the property names for a given shared object. ExampleThe following example calls getPropertyNames() on the myInfo shared object and places the names in the names variable. It then enumerates those property names in a for loop. myInfo = SharedObject.get("foo");
var addr = myInfo.getProperty("address");
myInfo.setProperty("city", "San Francisco");
var names = myInfo.getPropertyNames();
for (x in names){
var propVal = myInfo.getProperty(names[x]);
trace("Value of property " + names[x] + " = " + propVal);
}
SharedObject.handlerName()so.handlerName = function([p1,..., pN]){}
An event handler invoked when a shared object receives a message with the same name from the client-side SharedObject.send() method. You must define a Function object and assign it to the event handler. The this keyword used in the body of the function is set to the shared object instance returned by SharedObject.get(). If you don’t want the server to receive a particular message, do not define this handler. SharedObject.isDirtyso.isDirty Read-only; a boolean value indicating whether a persistent shared object has been modified since the last time it was stored (true) or not (false). The SharedObject.commit() method stores shared objects with an isDirty property that is true. This property is always false for nonpersistent shared objects. SharedObject.lock()so.lock() Locks a shared object. This method gives the server-side script exclusive access to the shared object; when the SharedObject.unlock() method is called, all changes are batched and one update message is sent through the SharedObject.onSync() handler to all the clients that subscribe to this shared object. If you nest the SharedObject.lock() and SharedObject.unlock() methods, make sure that there is an unlock() method for every lock() method; otherwise, clients are blocked from accessing the shared object. You cannot use the SharedObject.lock() method on proxied shared objects. SharedObject.mark()so.mark(handlerName, p1, ..., pN) Delivers all change events to a subscribing client as a single message. In a server-side script, you can call the SharedObject.setProperty() method to update multiple shared object properties between a call to the lock() and unlock() methods. All subscribing clients receive a change event notification through the SharedObject.onSync() handler. However, because the server may collapse multiple messages to optimize bandwidth, the change event notifications may not be sent in the same order as they were in the code. Use the mark() method to execute code after all the properties in a set have been updated. You can call the handlerName parameter passed to the mark() method, knowing that all property changes before the mark() call have been updated. Parameters
ReturnsA boolean value. Returns true if the message can be dispatched to the client; otherwise, false. ExampleThe following example calls the mark() method twice to group two sets of shared object property updates for clients: var myShared = SharedObject.get("foo", true);
myShared.lock();
myShared.setProperty("name", "Stephen");
myShared.setProperty("address", "Xyz lane");
myShared.setProperty("city", "SF");
myShared.mark("onAdrChange", "name");
myShared.setProperty("account", 12345);
myShared.mark("onActChange");
myShared.unlock();
The following example shows the receiving client-side script: connection = new NetConnection();
connection.connect("rtmp://flashmediaserver/someApp");
var x = SharedObject.get( "foo", connection.uri, true);
x.connect(connection);
x.onAdrChange = function(str) {
// Shared object has been updated,
// can look at the "name", "address" and "city" now.
}
x.onActChange = function(str) {
// Shared object has been updated,
// can look at the "account" property now,
}
SharedObject.onStatus()so.onStatus = function(info) {}
Invoked when errors, warnings, and status messages associated with either a local instance of a shared object or a persistent shared object occur. ExampleThe following client-side code defines an anonymous function that just traces the level and code properties of the specified shared object: so = SharedObject.get("foo", true);
so.onStatus = function(infoObj){
//Handle status messages passed in infoObj.
trace(infoObj.level + "; " + infoObj.code);
};
SharedObject.onSync()so.onSync = function(list){}
Invoked when a shared object changes. Use the onSync() handler to define a function that handles changes made to a shared object by subscribers. For proxied shared objects, defines the function to get the status of changes made by the server and other subscribers. Note: You cannot define the onSync() handler
on the prototype property of the SharedObject class
in Server-Side ActionScript.
Parameters
ExampleThe following example creates a function that is invoked whenever a property of the shared object so changes: // Create a new NetConnection object.
nc = new NetConnection();
nc.connect("rtmp://server1.xyx.com/myApp");
// Create the shared object.
so = SharedObject.get("MasterUserList", true, nc);
// The list parameter is an array of objects containing information
// about successfully or unsuccessfully changed properties
// from the last time onSync() was called.
so.onSync = function(list) {
for (var i = 0; i < list.length; i++) {
switch (list[i].code ) {
case "success":
trace ("success");
break;
case "change":
trace ("change");
break;
case "reject":
trace ("reject");
break;
case "delete":
trace ("delete");
break;
case "clear":
trace ("clear");
break;
}
}
};
SharedObject.purge()so.purge(version) Causes the server to remove all deleted properties that are older than the specified version. Although you can also accomplish this task by setting the SharedObject.resyncDepth property, the purge() method gives the script more control over which properties to delete. SharedObject.resyncDepthso.resyncDepth An integer that indicates when the deleted properties of a shared object should be permanently deleted. You can use this property in a server-side script to resynchronize shared objects and to control when shared objects are deleted. The default value is infinity. If the current revision number of the shared object minus the revision number of the deleted property is greater than the value of SharedObject.resyncDepth, the property is deleted. Also, if a client connecting to this shared object has a client revision that, when added to the value of SharedObject.resyncDepth, is less than the value of the current revision on the server, all the current elements of the client shared object are deleted, the valid properties are sent to the client, and the client receives a “clear” message. This method is useful when you add and delete many properties and you don’t want to send too many messages to the client. Suppose that a client is connected to a shared object that has 12 properties and then disconnects. After that client disconnects, other clients that are connected to the shared object delete 20 properties and add 10 properties. When the client reconnects, it could, for example, receive a delete message for the 10 properties it previously had and then a change message on two properties. You can use SharedObject.resyncDepth property to send a “clear” message, followed by a change message for two properties, which saves the client from receiving 10 delete messages. SharedObject.send()so.send(methodName, [p1, ..., pN]) Executes a method in a client-side script. You can use SharedObject.send() to asynchronously execute a method on all the Flash clients subscribing to a shared object. The server does not receive any notification from the client on the success, failure, or return value in response to this message. Parameters
ExampleThe following example calls the SharedObject.send() method to invoke the doSomething() method on the client and passes the string "This is a test": var so = SharedObject.get("foo", true);
so.send("doSomething", "This is a test");
The following example is the client-side ActionScript code that defines the doSomething() method: nc = new NetConnection();
nc.connect("rtmp://www.adobe.com/someApp");
var so = SharedObject.getRemote("foo", nc.uri, true);
so.connect(nc);
so.doSomething = function(str) {
// Process the str object.
};
SharedObject.setProperty()so.setProperty(name, value) Updates the value of a property in a shared object. The name parameter on the server side is the same as an attribute of the data property on the client side. For example, the following two lines of code are equivalent; the first line is Server-Side ActionScript and the second is client-side ActionScript: so.setProperty(nameVal, "foo"); clientSO.data[nameVal] = "foo"; A shared object property can be modified by a client between successive calls to SharedObject.getProperty() and SharedObject.setProperty(). If you want to preserve transactional integrity, call the SharedObject.lock() method before modifying the shared object; be sure to call SharedObject.unlock() when you finish making modifications. If you call SharedObject.setProperty() without first calling SharedObject.lock(), the change is made to the shared object, and all object subscribers are notified before SharedObject.setProperty() returns. If you call SharedObject.lock() before you call SharedObject.setProperty(), all changes are batched and sent when the SharedObject.unlock() method is called. The SharedObject.onSync() handler on the client side is invoked when the local copy of the shared object is updated. Note: If only one source (whether client or server)
is updating a shared object in a server-side script, you don’t need
to use the lock() or unlock() method
or the onSync() handler.
Parameters
ExampleThe following example uses the SharedObject.setProperty() method to create the city property with the value San Francisco. It then enumerates all the property values in a for loop and calls trace() to display the values. myInfo = SharedObject.get("foo");
var addr = myInfo.getProperty("address");
myInfo.setProperty("city", "San Francisco");
var names = sharedInfo.getPropertyNames();
for (x in names){
var propVal = sharedInfo.getProperty(names[x]);
trace("Value of property " + names[x] + " = " + propVal);
}
SharedObject.unlock()so.unlock() Allows other clients to update the shared object. A call to this method also causes the server to commit all changes made after the SharedObject.lock() method is called and sends an update message to all clients. You cannot call the SharedObject.unlock() method on proxied shared objects. ReturnsAn integer indicating the lock count: 0 or greater if successful; -1 otherwise. For proxied shared objects, this method always returns -1. |