ActionScript 3.0 How can I send multidimensional array of objects over network

495 Views Asked by At

I am new to this part of AS 3.0. I found the example by Lee Burrows. That works fine, but I need to send a complex array of objects or at least multidimensional array. And I can't get it to work.

Struggling with it for a week. Can anyone help?

Here is the parts of the code:

 private function netHandler(event:NetStatusEvent):void
    {
        //update UI
        outputText.text +=  "[EVENT]\n" + event.info.code + "\n";
        //handle event
        switch (event.info.code)
        {
                //connection succeeded so setup a group
            case "NetConnection.Connect.Success" :


                ns1 = new NetStream(netConn);

                setupGroup();
                break;
                //group setup succeeded so enable submit
            case "NetGroup.Connect.Success" :
                submitButton.enabled = true;
                break;
                //posting received so add to output
            case "NetGroup.Posting.Notify" :


                var myObjectList:Array = new Array();
                myObjectList = event.info.message;
                dl.dataProvider = myObjectList[0];
                dl.rowCount = myObjectList[0].length;
                gd.dataProvider = myObjectList[1];
                gd.rowCount = myObjectList[1].length;
                outputText.text +=  "[RECEIVED]\n" + event.info.message.txt + "\n";
                break;
        }
    }

I getting error

TypeError: Error #1034: Type Coercion failed: cannot convert Object@7bc4dd9 to fl.data.DataProvider.

3

There are 3 best solutions below

2
Creative Magic On

It can be easily done if you convert your Array/Vector/Object to a JSON object that Flash now supports natively.

0
lbarbosa On

Have you considering using JSON?

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/JSON.html

Just use stringify() to convert it to JSON and parse() on the other side to decode it. If the "other side" is not AS3 you should be able to parse JSON easily in any language.

0
chadiik On

The way I'll do it is let a ByteArray object serialize the array to AMF (automatically), compress it and send it:

Sending data:

var byteArray:ByteArray = new ByteArray();
var array:Array = [[new Date(), 24], [new Shape(), 24.5], 1 / 2];
byteArray.writeObject(array);
byteArray.compress(CompressionAlgorithm.LZMA); //Optional
netGroup.sendTo-AllNeighbors/nearest/Neighbor(byteArray);

Receiving data:

//private function onReceiveData(data:Object):void 
var byteArray:ByteArray = data as ByteArray;
byteArray.uncompress(CompressionAlgorithm.LZMA); //if compressed
var array:Array = byteArray.readObject();
trace("@", getTimer(), ": ", array, "\n");

Output:

@2676: Sun Sep 22 02:41:36 GMT+0300 2013,24,[object Object],24.5,0.5

Of course if needed outside an AS3 application, you'll need to use third party libraries (there's many!) for the other language used.

Your other option would be to use JSON but I guess that data to transfer would be much larger and parsing it would take much more time (String manip. vs optimized access to binary data!)


EDIT: To serialize and read back custom classes (as a custom class and not generic Object) you should register a class alias beforehand like this:

import flash.net.registerClassAlias
registerClassAlias("com.CustomClass", CustomClass);