NetConnection, SharedObject, & NetStream’s client Property in ActionScript 3

Past two weeks, I’ve been porting over some Flashcom & Flex 1.5 code to Flex 2 (still using FCS 1.5 vs. FMS 2). One nice addition I found in the ActionScript 3 SharedObject class is the client property. This little gem allows you have events that are called by SharedObject.send to be called on a proxy class instead. What this means is, if someone in a Flashcom application causes the remote SharedObject to be updated, and it fires a “onChatMessageUpdate” method, it’ll actually get called on the client object if you set it to something. Since a lot of intrinsic classes are now “final”, meaning you can’t futz with ‘em as easily as you could in AS2, this is a great way to incorporate it via Composition.

var my_rso:SharedObject = SharedObject.getRemote("chat", nc.uri, false);
my_rso.owner = this;
my_rso.onChat = function(str)
{
        this.owner.onChatMessage(str);
};

function onChatMessage(str)
{
        textArea.text += str;
}

In the past, you’d use something like the above; proxying the message yourself, or even putting the logic inside the method closure if you were in a hurry.

Now, you can forward it to an anonymous object, yourself, or even a class instance.

var my_rso:SharedObject = SharedObject.getRemote("chat", nc.uri, false);
my_rso.client = this; // onChatMessage needs to be a public function in this class
my_rso.client = {onChatMessage: function(str){ // }}; // anonymous function
                my_rso.client = new ProxyClass(); // you can make a class specifically to handle events

David Simmons from Adobe told me it was on NetConnection as well. Just checked, and she’s on NetStream too. In my tests, for classes, it only works on methods in the public namespace; protected & private don’t work. I didn’t test custom namespaces, nor do I know how to make it “see” them.

Handy! Thanks Adobe.

6 Replies to “NetConnection, SharedObject, & NetStream’s client Property in ActionScript 3”

  1. Hi,
    thx, I have been trying to attach a method to the sharedObject class, which always throws a null pointer exception. Aren’t AS3 classes sealed ?
    Thanx for the reminder :)
    forgot about client….

  2. May I ask you about the send method.
    I cant get it to work ? problem is i always get a
    null pointer exception as mentioned above , scratch head ….
    How can I implement the callback ? Can you please give me a hint ?
    Thanx in advance :)

  3. 2 things.

    First, you need to set the client property to a class that has the methods. I do it to the class using the SharedObject, like this:

    users_so.client = this;

    Secondly, the methods on the class that the SharedObject is accessing must be public. If they are protected or private, you’ll get an exception.

  4. Hi
    Good to know you were successful to port your client actionsctipt 2.0 to 3.0
    I’m been trying to port a simple connection code from 2.0 to 3.0 and I keep getting connection failed in 3.0 whilst it connects fine in 2.0

    My 2.0 client-side code:
    //==================
    var VideoNC:NetConnection;

    VideoNC = new NetConnection;
    VideoNC.onStatus = function(info) {

    trace(info.code);
    if (info.code == “NetConnection.Connect.Success”) {

    trace(“connected”);
    } else if (info.code == “NetConnection.Connect.Closed”) {

    }
    }

    VideoNC.connect(“rtmp://10.211.55.6/learningObjects/”);
    //This is my local FMS server on my parallels desktop
    //==================

    I get connection success info code with this

    My AS 3.0 client-side code
    //==================
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.events.*;

    var connection:NetConnection;
    var stream:NetStream;

    connection = new NetConnection();

    connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);

    function netStatusHandler(event:NetStatusEvent):void {

    trace(event.info.code);
    if (event.info.code == “NetConnection.Connect.Success”) {
    //connection.connect();
    trace(“connect to”);
    }

    }

    connection.connect(“rtmp://10.211.55.6/learningObjects/”);
    //==================

    I get connection Failed

    My main.asc file in my FMS app folder is:
    //==================
    application.onAppStart = function () {

    trace(“load new app”);
    }

    application.onConnect = function (client) {

    trace(“connect “+client.ip);

    application.acceptConnection(client);

    return true;

    }

    application.onDisconnect = function(client) {

    trace(“disconnect “+client.ip);

    }
    //==================

    Do you have any ideas?
    Thanks

  5. First… I am a flash/AS3 newb.

    Secondly, thank you so much for posting a clear answer to this issue. In the hours that I spent trying to research “shared object callback AS3” I could not find any clear answer.

  6. Dude, thanks!
    I’ve had lot of trouble setting up the remote SO message sending stuff, the adobe docs didn’t even compile.

Comments are closed.