Shared objects
About shared objectsUse shared
objects to synchronize users and store data. Shared objects can
do anything from holding the position of pieces on a game board
to broadcasting chat text messages. Shared objects let you keep
track of what users are doing in real time.
With Flash Media Interactive Server or Flash Media Development
Server, you can create and use remote shared objects, which
share data between multiple client applications. When one user makes
a change that updates the shared object on the server, the shared
object sends the change to all other users. The remote shared object
acts as a hub to synchronize many users. In the section SharedBall example,
when any user moves the ball, all users see it move.
Note: Flash Media Streaming Server does not support
remote shared objects.
All editions of
the server support local shared objects, which are similar
to browser cookies. Local shared objects are stored on the client
computer and don’t require a server.
Shared objects, whether local or remote, can also be temporary or persistent:
A temporary shared object is created by a server-side
script or by a client connecting to the shared object. When the
last client disconnects and the server-side script is no longer
using the shared object, it is deleted.
Persistent shared objects retain data after all clients disconnect
and even after the application instance stops running. Persistent
shared objects are available on the server for the next time the
application instance starts. They maintain state between application
sessions. Persistent objects are stored in files on the server or
client.
- Persistent local shared objects
- To create persistent local shared objects, call the client-side SharedObject.getLocal() method.
Persistent local shared objects have the extension .sol. You can
specify a storage directory for the object by passing a value for
the localPath parameter of the SharedObject.getLocal() command.
By specifying a partial path for the location of a locally persistent
remote shared object, you can let several applications from the
same domain access the same shared objects.
- Remotely persistent shared objects
- To create remote shared objects that are persistent on the
server, pass a value of true for the persistence parameter
in the client-side SharedObject.getRemote() method
or in the server-side SharedObject.get() method.
These shared objects are named with the extension .fso and are stored
on the server in a subdirectory of the application that created
the shared object. Flash Media Server creates these directories
automatically; you don’t have to create a directory for each instance
name.
- Remotely and locally persistent shared objects
- You create remote shared objects that are persistent on the
client and the server by passing a local path for the persistence
parameter in your client-side SharedObject.getRemote() command.
The locally persistent shared object is named with the extension
.sor and is stored on the client in the specified path. The remotely
persistent .fso file is stored on the server in a subdirectory of
the application that created the shared object.
Remote shared objectsBefore 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. The general sequence of steps for using a
remote shared object is outlined below:
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 the NetConnection object and define
event handler methods. For more information, see SharedBall example.
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 are connecting to and must be
identical to the URI used in the NetConnection.connect() method.
The easiest way 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.
Connect to the remote shared object. Once the shared object
is created, connect the client to the shared object using 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);
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.
To update the remote shared object
when a client makes a change, use setProperty():
so.setProperty("x", sharedBall.x);
You
must use setProperty() to update values in the
shared object. The remote shared object has a data property
that contains attributes and values. However, in ActionScript 3.0,
you cannot write values directly to it, as in:
so.data.x = sharedBall.x; // you can't do this
When the shared object is updated, it dispatches a sync event. Synchronize
the change to the remaining clients by reading the value of the shared
object’s data property:
sharedBall.x = so.data.x;
This
is usually done in a sync event handler, as shown
in SharedBall example.
SharedBall exampleThe SharedBall sample creates a temporary remote shared
object. It’s similar to a multiplayer game. When one user moves
the ball, it moves for all other users.
Note: Use the SharedBall sample files (SharedBall.fla,
SharedBall.as, and SharedBall.swf) in the documentation/samples/SharedBall
directory in the Flash Media Server root install directory.
 The SharedBall application running in Flash Player Run the applicationRegister the application with
your server by creating an application directory named SharedBall:
RootInstall/applications/SharedBall
Open the SharedBall samples files from the documentation/samples/SharedBall
directory in the Flash Media Server root install directory.
Open SharedBall.swf in a web browser.
Open a second instance of SharedBall.swf in a second browser
window.
Move the ball in one window and watch it move in the other.
Design the user interfaceIn Flash, choose File >
New > Flash File (ActionScript 3.0) and click OK.
From the toolbox, select the Rectangle tool. Drag to the
lower-right corner, then select the Oval tool.
Draw a circle on the Stage. Give it any fill color you like.
Double-click the circle and choose Modify > Convert
to Symbol.
In the Convert to Symbol dialog box, name the symbol ball,
check that Movie Clip is selected, and click OK.
Select the ball symbol on the Stage and in the Property Inspector
(Window > Properties) give it the instance name sharedBall.
Save the file as SharedBall.fla.
Write the client-side codeBe sure to look at the SharedBall.as
sample file. These steps present only highlights.
In Flash Professional, create a new ActionScript file.
Create the class, extending MovieClip:
public class SharedBall extends MovieClip {...}
The
class must extend MovieClip, because the sharedBall symbol in the
FLA file is a Movie Clip symbol.
Create the constructor, in which you add event listeners
and connect to the server:
public function SharedBall()
{
nc = new NetConnection();
addEventListeners();
nc.connect("rtmp://localhost/SharedBall");
}
Add event listeners for netStatus, mouseDown, mouseUp,
and mouseMove events:
private function addEventListeners() {
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
// sharedBall is defined in the FLA file
sharedBall.addEventListener(MouseEvent.MOUSE_DOWN, pickup);
sharedBall.addEventListener(MouseEvent.MOUSE_UP, place);
sharedBall.addEventListener(MouseEvent.MOUSE_MOVE, moveIt);
}
In your netStatus handler, create a remote
shared object when a connection is successful. (You’ll also want
to create error handlers for rejected and failed connections, shown
in the sample AS file.) Connect to the shared object and add a sync event
listener:
switch (event.info.code)
{
case "NetConnection.Connect.Success":
trace("Congratulations! you're connected");
so = SharedObject.getRemote("ballPosition", nc.uri, false);
so.connect(nc);
so.addEventListener(SyncEvent.SYNC, syncHandler);
break;
...
}
As a user moves the mouse, use setProperty() to
set the changing ball location in the remote shared object:
function moveIt( event:MouseEvent ):void {
if( so != null )
{
so.setProperty("x", sharedBall.x);
so.setProperty("y", sharedBall.y);
}
}
When the remote shared object is updated, it dispatches
a sync event.
Write a sync event handler that updates
all clients with the new ball position:
private function syncHandler(event:SyncEvent):void {
sharedBall.x = so.data.x;
sharedBall.y = so.data.y;
}
You can read the value of so.data,
even though you can’t write to it.
Broadcast messages to many usersA remote
shared object allows either a client or server to send a message
using SharedObject.send() to all clients connected
to the shared object. The send() method can be
used for text chat applications, for example, where all users subscribed
to your shared object receive your message.
When you use SharedObject.send(), you, as broadcaster,
also receive a copy of the message.
Write a method that SharedObject.send() will
call:
private function doSomething(msg:String):void {
trace("Here's the message: " + msg);
}
Call send() to broadcast the message:
so = SharedObject.getRemote("position", nc.uri, false);
so.connect(nc);
so.send("doSomething", msg);
|
|