0

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

mizi_sk
  • 1,007
  • 7
  • 33

2 Answers2

1

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

mizi_sk
  • 1,007
  • 7
  • 33
  • This only works if you already know what type to expect. What if you have large amounts of data? – weltraumpirat Feb 23 '11 at 00:20
  • @weltraumpirat when you send all data types in one relay method you have two options: 1) `_nc.call("echo", null, "ComplexTypeAlias", ba)` and `ba.readObject() as getClassByAlias(arg1)` or 2) wrap your objects in another, `class RelayMessage { public var _payload:Object; }` which deserializes content as well – mizi_sk Feb 23 '11 at 07:55
  • @weltraumpirat _What if you have large amounts of data?_ Are there any issues with performance when sending ByteArray? – mizi_sk Feb 23 '11 at 09:28
  • 1
    @Thomas: No, ByteArray is fine. It's more an issue of reusability: With your solution, you'll have one module capable of sending and receiving one type of message (for which you have a message class). It would not, however, be reusable for another project; you'd have to write a different message class. There should be a way to do this for any kind of data, as long as it's serializable, and I always thought the alias thing was the way to do it. – weltraumpirat Feb 23 '11 at 09:38
  • @weltraumpirat it seems that I can use NetStream publish/play with server and works the same – mizi_sk May 12 '11 at 21:52
  • @Tomas: I was going in a different direction. You type cast to ComplexType, because you know you are going to get exactly that class. "Large amounts of data" was referring to a large number of small objects of different types, some of which you might not even know in advance. If you use your solution, you will have to change the transmit and receive code each time the transferred data content changes - a more generic solution would be reusable as a component. – weltraumpirat May 12 '11 at 22:45
  • This was supposed to be a fallback (server) solution for P2P low latency game, so NetStream or relay with NetConnection was sufficient. I don't have clients with different versions, they are the same SWF, so they "know" the types. – mizi_sk May 12 '11 at 22:55
  • What about the next game, then? – weltraumpirat May 13 '11 at 00:07
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.

weltraumpirat
  • 22,544
  • 5
  • 40
  • 54
  • it's not necessary to alias class if you are not using BlazeDS (or compatible) server side. I am just relaying message by client-server-client connections. – mizi_sk Feb 23 '11 at 07:57