Skip to main content
Participating Frequently
March 29, 2010
Question

Simple SharedObject Help

  • March 29, 2010
  • 1 reply
  • 1466 views

Hi,

I'm trying to use SharedObjects to manage some state between my server and clients, and am having a lot of trouble understanding the documentation and can't find any examples that work. Here is what I'm trying to do:

1. Create a shared object on my server in an application instance running on my FMS.

2. Allow clients to receive updates to the state of this shared objects.

Could someone simply show me the relevant api calls that will allow me to:

1. Create a shared object on my server instance.

2. Set up the proper calbacks so that a client can receive updates to this object.

I suspect that the number of lines involved to accomplis this is < 10, but the examples I've found in the documentation produce compilation errors.

Thank you so much.

    This topic has been closed for replies.

    1 reply

    March 30, 2010

    Hi,

    but the examples I've found in the documentation produce compilation errors.

    Can you please tell what kind of compilation errors did you get when you compiled your code?

    Now coming back to your requirements

    1. Create a shared object on my server instance.

    It depends whether you want to have persistent or non-persistent shared object. You can use SharedObject.getRemote() method to create the shared object on your server instance.

    For ex.

    var my_nc:NetConnection = new NetConnection();

    my_nc.connect("rtmp://somedomain.com/applicationName");

    var myRemote_so = SharedObject.getRemote("mo", my_nc.uri, false);

    This will create a ahared object on the default instance of the applicationName on the server. The third parameter is persistence which is set to false. Thus as soon as all clients disconnect, the shared object will be deleted.

    Now you call

    myRemote_so.connect(nc);

    This will connect your shared object to the remote shared object on FMS through the specified connection.

    2. Set up the proper calbacks so that a client can receive updates to this object

    To achieve this you can use the onSync handler. (I am assuming that you are using AS2, and in case you are using AS3, you may use the SyncEvent.SYNC ). This onSync Handler is invoked whenever there is any change made to shared object. In this handler you can involve the logic to update whatever data you want here. Here you can find which property has been updated on the shared object and thus can accordingly change your logic. You can refer the documentation for more info on this handler.

    Now all the information I provided you are general information and it may be possible that you may be already knowing it and you may be facing issue in doing this or you may have some special requirements and are not sure how to go about it.. If that is the case you can provide me the details as to what issues are you running into or what you are actually trying to do which is not working, so that I can answer you better.

    Thanks,

    Abhishek

    jimt899Author
    Participating Frequently
    March 30, 2010

    "Can you please tell what kind of compilation errors did you get when you compiled your code?"

    Here's one example from the FMS docs regarding obtaining a shared object in a client:

    var mync = new NetConnection();

    mync.connect("rtmp://ipaddr/appname");

    var myso = SharedObject.get("myso", true, mync);

    The last line produces:

    1061: Call to a possibly undefined method get through a reference with static type Class.

    I'm sure it's an easy fix, but I am new to Actionscript, and find it odd that an interpreted language would produce compilation erros such as this when copied verbatim from the documentation.

    ---------

    Thanks for the suggestions. Here's what I have in my server (main.asc):

    application.onAppStart = function()

    {

            this.my_nc = new NetConnection();

            this.my_nc.connect("rtmp://<ip addr>/<app name>");

            this.my_so =  SharedObject.getRemote("myso", this.my_nc.uri, true);

            this.my_so.connect(this.my_nc);

    ...

    I assume this creates a (persistent) shared object that I can monitor from my client.

    In my client, I'm doing something like this:

    var mync:NetConnection = new NetConnection();

    mync.connect("rtmp://<ip addr>/<app name>");

    var myso:SharedObject = SharedObject.get("myso", true, mync);

    myso.addEventListener(SyncEvent.SYNC, onSOChange);

    Which I want to give me a callback when a change is made to my shared object on the server. However, I continue to get the compilation error I presented previously, which makes me wonder if this is the correct approach.

    One last question regarding shared objects:

    What does it mean to 'update' a shared object? What sequence of events are required to change the state of an object, and what state can be changed? Are shared object *properties* the things that change and are tracked by clients?

    Thanks again.

    March 30, 2010

    On the client side, use SharedObject.getRemote() . That's the cause of your RTE.