Simon Washington
Simon Washington

Reputation: 31

How to use Remote Shared Objects

I'm trying to use a remote shared object to send data messages between the client and server using the sharedobject.send method for a chat application. I am really having difficulty understanding the documentations. The code i attempted to use simply didn't work.

Can someone be kind enough to show me the relevant API calls? And, take a close inspection at my code and tell me were exactly i am going wrong?

Thank You.

Client-Side

import flash.net.NetConnection;
import flash.events.NetStatusEvent;
import flash.events.SyncEvent;

var nc:NetConnection = new NetConnection();

nc.connect("rtmfp://fms/exampledomain");

nc.addEventListener(NetStatusEvent.NET_STATUS, netHandler);

function netHandler(event:NetStatusEvent):void {
    if (event.info.code == "NetConnection.Connect.Success") {
        trace("Your Connected");

        //Creating the sharedobject
        var my_so:SharedObject = SharedObject.getRemote("users", nc.uri, false);

        my_so.connect(nc);

        my_so.addEventListener(SyncEvent.SYNC, syncHandler);

        my_so.setProperty("users");

        my_so.setDirty("users");

        function syncHandler(event:SyncEvent):void {
            var changelist:Array = event.changeList;
        }

        my_so.send function sendMessage(str){

Server-Side

application.onConnect(clientObj)() {
    application.acceptConnection(clientObj) {
        trace("connected up");
    }

    var my_so = SharedObject.getRemote("users", false);
    my_so.send("Hello user", "Its only a test");
}

Upvotes: 1

Views: 1757

Answers (1)

duTr
duTr

Reputation: 714

After editing your question, it looks like some code is missing in your client-side sample.

From what I see, you don't need the client-side code for your example to work. You can call the send() method directly from the client. But to test whether it works or not, you will need to have two clients connected.

About your NetConnection or your SharedObject, make sure you add your event listeners before you call the connect() method.

If what you want to achieve is simply sharing data between clients, then you don't even need to use the send() method but simply set properties on your shared object. But be aware of the following:

my_so.setProperty("users");

is actually deleting the key users as it is the same as this:

my_so.setProperty("users", null);

Finally, you would need to call setDirty() in very specific cases, but not in this one. Have a look at this answer for a more detailed example on how to share data with SharedObjects

Upvotes: 2

Related Questions