SharedObject with Flash Media Server

SharedObject is a feature that give Flash Player the ability to save data locally to be used later in your application, roughly speaking is like a cookie. With Flash Media Server, was introduced the possibility of using the SharedObject Remote, in which the data is saved in Flash Media Server and shared for all instances of a connection between Flash Player and Flash Media Server. This enables you to create applications in real time as Dashboard, chats and whatever else your imagination allows.

The SharedObject Remote can be used in two ways in conjunction with Adobe Flash Media Server.

  • Only the client side, via the Flash Player/AIR
  • Together with ActionScript Communication, server-side programmgin in Flash Media Server

Consider the first case:

Through the client side, via the Flash Player/AIR

If you have no knowledge to use the language of the Server-side Flash Media Server, you can use the SharedObject Remote only by ActionScript 3.0. Let’s sharedBall a classic example, where the goal is to share the positions of the ball to update the other clients connected to the same instance.

  • Go to the installation directory of the Flash Media Server and browse to the folder “applications”, there create a directory called “sharedBall” inside a file called “main.asc”
  • Open main.asc in your text editor or preferably by the Flash and enter: trace (“sharedBall …”);
  • Let’s test this file, open the Flash Media Server console, usually located in the installation directory / webrrot / swfs. You can open the browser or the swf directly.
  • Click the “View Applications” and then search for the name “sharedBall” in the combobox at the very bottom left corner where it says “New Instance …”
  • Select “sharedBall” tab and we should have something like Live Log in the image below:

  • In Flash, I created a ball with the drawing tools and converted to give the name of MovieClip “mc_ball.”
  • We are ready to begin the integration between Flash and Flash Media Server. Create a layer for ActionScript and open the editor by pressing F9, or “Window-> Actions”
  • Our code begins with the connection to the server Flash Media Server

[ACTIONSCRIPT3]
import flash.net.NetConnection;
import flash.events.NetStatusEvent;

var nc:NetConnection;

function init():void
{
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, handlerNetStatus);
nc.connect(“rtmp://localhost/sharedBall”);
}

function handlerNetStatus(evt:NetStatusEvent):void
{
trace(evt.info.code);
}

init();
[/ACTIONSCRIPT3]
That done, we can test the swf by pressing Ctrl + Enter, if all goes well, we have the following message in the output Flash:

NetConnection.Connect.Success

With the connection made, we can instantiate our Remote SharedObject to share the x and y positions of the ball.
[ACTIONSCRIPT3]
if(evt.info.code == “NetConnection.Connect.Success”)
{
so = SharedObject.getRemote(“so”,nc.uri,false);
so.addEventListener(NetStatusEvent.NET_STATUS, handlerNetStatus);
so.addEventListener(SyncEvent.SYNC, handlerSync);
so.connect(nc);
}
[/ACTIONSCRIPT3]
The handlerSync is responsible for updating the data of x and y we will take the SharedObject Remote:
[ACTIONSCRIPT3]
function handlerSync(evt:SyncEvent):void
{
mc_ball.x = so.data.x;
mc_ball.y = so.data.y;
}
[/ACTIONSCRIPT3]
We will give you the option to click on the ball, it can be draggable, then x and y positions will update the SharedObject Remote:
[ACTIONSCRIPT3]
mc_ball.addEventListener(MouseEvent.MOUSE_DOWN, handlerSharedBall);
mc_ball.addEventListener(MouseEvent.MOUSE_UP, handlerSharedBallOut);

function handlerSharedBall(evt:MouseEvent):void
{
this.addEventListener(Event.ENTER_FRAME, update);
mc_ball.startDrag();
}

function handlerSharedBallOut(evt:MouseEvent):void
{
mc_ball.stopDrag();
}

function update(evt:Event=null):void
{
so.setProperty(“x”,mc_ball.x);
so.setProperty(“y”,mc_ball.y);
}
[/ACTIONSCRIPT3]

Following is the complete code:
[ACTIONSCRIPT3]
import flash.net.NetConnection;
import flash.events.NetStatusEvent;
import flash.net.SharedObject;
import flash.events.SyncEvent;
import flash.events.MouseEvent;
import flash.events.Event;

var nc:NetConnection;
var so:SharedObject;

function init():void
{
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, handlerNetStatus);
nc.connect(“rtmp://localhost/sharedBall”);
}

function handlerNetStatus(evt:NetStatusEvent):void
{
trace(evt.info.code);
if(evt.info.code == “NetConnection.Connect.Success”)
{
so = SharedObject.getRemote(“so”,nc.uri,false);
so.addEventListener(NetStatusEvent.NET_STATUS, handlerNetStatus);
so.addEventListener(SyncEvent.SYNC, handlerSync);
so.connect(nc);
}
}

function handlerSync(evt:SyncEvent):void
{
mc_ball.x = so.data.x;
mc_ball.y = so.data.y;
}

mc_ball.addEventListener(MouseEvent.MOUSE_DOWN, handlerSharedBall);
mc_ball.addEventListener(MouseEvent.MOUSE_UP, handlerSharedBallOut);

function handlerSharedBall(evt:MouseEvent):void
{
this.addEventListener(Event.ENTER_FRAME, update);
mc_ball.startDrag();
}

function handlerSharedBallOut(evt:MouseEvent):void
{
mc_ball.stopDrag();
}

function update(evt:Event=null):void
{
so.setProperty(“x”,mc_ball.x);
so.setProperty(“y”,mc_ball.y);
}

init();
[/ACTIONSCRIPT3]
See a demonstration in operation:

More source and PDFs click here

Was this article helpful? feel free to make a donation and help keep the blog in the air
ActionScript 3.0, Flash, Flash Media Server

Leave a Reply