Sei sulla pagina 1di 5

Shared Object in flex

Local Shared Objects


The SharedObject class functions like a browser cookie. You use the class to store data on the user's local hard drive and call that data during the same session or in a later session. Applications can only access their own SharedObject data, and only if they are running on the same domain. The data is not sent to the server and is not accessible by other Flex applications running on other domains. SharedObject.clear() Purges all of the data from the shared object, and deletes the shared object from the disk. SharedObject.flush() Immediately writes a locally persistent shared object to a local file. SharedObject.getLocal() Returns a reference to a locally persistent shared object that is available only to the current client. SharedObject.getSize() Gets the current size of the shared object, in bytes. 1. Creating Shared Object To create a SharedObject object, use the SharedObject.getLocal() method, which has the following syntax:SharedObject.getLocal("objectName" [, pathname]): SharedObject The following example creates a shared object called mySO: public var mySO:SharedObject; mySO = SharedObject.getLocal("preferences",/); This creates a file on the client's machine called preferences.sol.The term local refers to the location of the shared object. Better initialize the shared Object to null always to avoid overwriting issues in the future i.e public var mySO:SharedObject=null; The default storage location for Local Shared Objects is operating system-dependent. On Microsoft Windows NT 5.x, they are stored in: %APPDATA%\Macromedia\Flash Player\#SharedObjects\ %APPDATA%\Macromedia\Flash Player\macromedia.com\support\flashplayer\sys\ 2. Adding data to a shared object

You add data to a SharedObject's *.sol file using the data property of the SharedObject object. To add new data to the shared object, use the following syntax: sharedObject_name.data.variable = value; After you assign values to the data property, you must instruct Flash Player to write those values to the SharedObject's file. To force Flash Player to write the values to the SharedObject's file, use the SharedObject.flush() method, as follows: mySO.flush(); 3. Creating multiple shared objects You can create multiple shared objects for the same Flex application. To do this, you assign each of them a different instance name, as the following example shows: public var mySO:SharedObject = SharedObject.getLocal("preferences"); public var mySO2:SharedObject = SharedObject.getLocal("history"); This creates a preferences.sol file and a history.sol file in the Flex application's local directory.

4. Destroying shared objects To destroy a SharedObject on the client, use the SharedObject.clear() method. This does not destroy directories in the default path for the application's shared objects. The following example deletes the SharedObject file from the client: public function destroySharedObject():void { mySO.clear(); } NB: Each time you have to open the Shared Object using getlocal() method and after each flush the object must be closed and made null. mySO.close(); mySO=null;

Remote Shared objects


To connect to Flash Media Server, pass the URI of the application on the server. Use the following syntax (items in brackets are optional): protocol:[//host][:port]/appname[/instanceName]

Use one of the following protocols: rtmp, rtmpe, rtmps, rtmpt, rtmpte, or rtmfp. If the connection is successful, a netStatus event with a code property of NetConnection.Connect.Success is returned. See the NetStatusEvent.info property for a list of all event codes returned in response to calling connect(). If the file is served from the same host where the server is installed, you can omit the //host parameter. If you omit the /instanceName parameter, Flash Player or AIR connects to the application's default instance. (Flash Player 10.1 or AIR 2 or later)To create peer-to-peer applications, use the rtmfp protocol.

http://www.scribd.com/doc/54127748/43/Shared-objects Page 52 Remote shared objects Before you create a remote shared object, create a NetConnection object and connect to the server. Once you have the connection, use the methods in the SharedObject class to create and update the remote shared object. Thegeneral sequence of steps for using a remote shared object is outlined below: 1. Create a NetConnection object and connect to the server: nc = new NetConnection();nc.connect("rtmp://localhost/SharedBall"); This is the simplest way to connect to the server. In a real application, you would add event listeners on theNetConnection object and define event handler methods. For more information, seeSharedBall example. 2. Create the remote shared object. When the connection is successful, call SharedObject.getRemote() To create a remote shared object on the server: so = SharedObject.getRemote("ballPosition", nc.uri, false); The first parameter is the name of the remote shared object. The second is the URI of the application you areconnecting to and must be identical to the URI used in the NetConnection.connect() method. The easiestway to specify it is with the nc.uri property. The third parameter specifies whether the remote shared object is persistent. In this case, false is used to make the shared object temporary.

3. Connect to the remote shared object. Once the shared object is created, connect the client to the shared objectusing the NetConnection object you just created so.connect(nc); You also need to add an event listener for sync events dispatched by the shared object: so.addEventListener(SyncEvent.SYNC, syncHandler); 4. Synchronize the remote shared object with clients. Synchronizing the remote shared object requires two steps. First, when an individual client makes a change or sets a data value, you need to update the remote shared object.Next, update all other clients from the remote shared object. a To update the remote shared object when a client makes a change, usesetProperty(): so.setProperty("x", sharedBall.x) http://help.adobe.com/en_US/FlashMediaServer/3.5_SS_ASD/WS5b3ccc516d4fbf351e63e3d11a11afc95 e-7e63.html The Server-Side ActionScript method SharedObject.get() creates remote shared objects; there is no server-side method that creates local shared objects. Local shared objects are stored in memory, unless theyre persistent, in which case they are stored in .sol files. Remote shared objects that are stored on the server have the file extension .fso and are stored in a subdirectory of the application that created them. Remote shared objects on the client have the file extension .sor and are also stored in a subdirectory of the application that created them. Server-side shared objects can be nonpersistent (that is, they exist for the duration of an application instance) or persistent (that is, they are stored on the server after an application closes). To create a persistent shared object, set the persistence parameter of the SharedObject.get() method to true. Persistent shared objects let you maintain an applications state.

RTMP This is the standard, unencrypted Real-Time Messaging Protocol. The default port is 1935; if a port is not specified, the client attempts to connect to ports in the following order: 1935, 443, 80 (RTMP), 80 (RTMPT). Only Flash Media Interactive Server can be configured as an edge server. Flash Media Streaming Serverand Flash Media Development Server cannot be configured as edge servers. Flash Media Server is a hub. Flash based applications connect to the hub using Real Time Messaging Protocol (RTMP). The server can send and receive data to and from the connected users with live web FLV player installed. Connected clients can make Remote procedure calls(RPC) on the server-side and

the server can call methods on specific clients. A SharedObject can be used to synchronize complicated data structures and call remote methods on multiple clients in one go by having clients subscribe to a shared object. Standard ActionScript objects are transported across the NetConnection using the Action Message Format (AMF) which is handled transparently by the server and flash client. The server also allows users to receive and publish net streams. When viewing a net stream the user can either create their own to view a remotely stored Flash Video (FLV) or the server can instantiate a net stream and play a video on it, the latter method means that every user will be at the same point in the video when they subscribe to it as they are all viewing the same stream. Normally when you connect to a Red5 app you connect by typing rtmp://server-name/app-name as the URL. When you want the client application to connect to your app and go into a room you type the same thing except you will add a room name. An example would be rtmp://server-name/app-name/room.

Potrebbero piacerti anche