Remote shared objects let you keep track, store and share/synchronize of data between multiple client applications. When one client changes data that updates the shared object on the server, the shared object sends the change to all other connected clients.
Create a NetConnection object and connect to the server. After the connection is established call SharedObject.getRemote(rso_name, uri, persistent) to create a remote shared object on the server. Once the shared object is created, connect the client to the shared object.
nc = new NetConnection();
nc.connect("rtmp://localhost/SharedBall");
so = SharedObject.getRemote("ballPosition", nc.uri, false);
so.connect(nc);
so.addEventListener(SyncEvent.SYNC, syncHandler);
...
private function syncHandler(event:SyncEvent):void {
sharedBall.x = so.data.x;
sharedBall.y = so.data.y;
}
When dealing with shared objects in server-side code, special care must be taken about the scope they are created in. To create a new shared object when a room is created (e.g. rtmp://server/application/room1), you can override the RoomStart method in your application. If a shared object should be created for connections to the main application (e.g. rtmp://server/application), override the AppStart method in your application.
public class Application : ApplicationAdapter
{
public bool RoomStart(IScope room)
{
if (!base.RoomStart(room))
return false;
CreateSharedObject(room, "sampleSO", true);
ISharedObject so = GetSharedObject(room, "sampleSO");
return true;
}
}
To get notified about changes of the shared object similar to onSync in FCS / FMS, a listener must implement the ISharedObjectListener interface.
public class SampleSharedObjectListener : ISharedObjectListener
{
public void OnSharedObjectUpdate(ISharedObject so, string key, object value)
{
// The attribute <key> of the shared object <so>
// was changed to <value>.
}
public void OnSharedObjectDelete(ISharedObject so, string key)
{
// The attribute <key> of the shared object <so> was deleted.
}
public void OnSharedObjectSend(ISharedObject so, String method, IList parameters)
{
// The handler <method> of the shared object <so> was called
// with the parameters <params>.
}
}
Additionally, the listener must be registered with the shared object:
ISharedObject so = GetSharedObject(scope, "sampleSO"); so.AddSharedObjectListener(new SampleSharedObjectListener());
To update the remote shared object use the SetAttribute method.
ISharedObject so = GetSharedObject(scope, "sampleSO");
so.SetAttribute("fullname", "Sample user");
If multiple actions on a shared object should be combined in one update event to the subscribed clients, use the BeginUpdate and EndUpdate methods
ISharedObject so = GetSharedObject(scope, "sampleSO");
so.BeginUpdate();
so.SetAttribute("One", "1");
so.SetAttribute("Two", "2");
so.RemoveAttribute("Three");
so.EndUpdate();
The server-side listeners will receive their update notifications through separate method calls as without the BeginUpdate and EndUpdate.
Calls to shared object handlers through remote_so.send(<handler>, <args>) from the client or the corresponding server-side call can be mapped to methods. Therefore a handler must be registered (through the ISharedObjectHandlerProvider interface):
class MySharedObjectHandler
{
public void MyMethod(string arg1)
{
}
}
...
ISharedObject so = GetSharedObject(scope, "sampleSO");
so.RegisterServiceHandler(new MySharedObjectHandler());