How to relay complex type via NetConnection to FMS?

那年仲夏 提交于 2020-01-06 04:56:45

问题


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!!!!



来源:https://stackoverflow.com/questions/5082433/how-to-relay-complex-type-via-netconnection-to-fms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!