How to relay complex type via NetConnection to FMS

2019-08-21 02:36发布

问题:

I need to send complex type object (marked RemoteClass in Flex) via NetConnection to other clients.

[RemoteClass]
public class ComplexType
{
    public var _someString:String;
    public var _someInt:int;
}

... and using ...

_nc = new NetConnection();
_nc.connect("rtmp://localhost/echo/");
_nc.addEventListener(NetStatusEvent.NET_STATUS, _onNetStatus);              
_nc.client = {};
_nc.client.echoCallback = _echoCallback;

var dto:ComplexType = new ComplexType();
dto._someInt = 4;
dto._someString = "abrakadabra";                    
_nc.call("echo", null, dto);

However it seems, that callback function on server side don't understand strongly typed objects and sends back this:

private function _echoCallback(...args):void
{
    trace(ObjectUtil.toString(args));
    /*

    (Array)#0
      [0] (Object)#1
         _someInt = 4
         _someString = "abrakadabra"

    */
}

Server side looks like this:

application.onAppStart = function () {
    trace("Application.onAppStart > application started");

    Client.prototype.echo = function (complexType /*ComplexType*/) {
        trace("Client.echo > calling echo");        
        application.broadcastMsg("echoCallback", complexType);
    }
}

Is there a way to relay strongly typed object via NetConnection?

EDIT1: added callback function source code with ObjectUtil.toString() output

回答1:

You need to add an alias property to your [RemoteClass] annotation:

[RemoteClass(alias="my.unique.Class")]

This should change the anonymous object to a typed object in AMF.



回答2:

For sending use:

var ba:ByteArray = new ByteArray();
ba.writeObject(dto);

_nc.call("echo", null, ba);

And for receiving:

private function _echoCallback(ba:ByteArray):void
{
    var dto:ComplexType = ba.readObject() as ComplexType;

    trace(ObjectUtil.toString(dto));

    /*
    (ComplexType)#0
      _someInt = 4
      _someString = "abrakadabra"
    */
}

It woooorks!!!!