Skip to main content
Known Participant
June 8, 2010
Question

How exactly to work with a ByteArray object received by FMS from a client?

  • June 8, 2010
  • 1 reply
  • 1259 views

It's a bit vague - the whole thing with AMF. On one hand we have a client capable of serializing and sending everything from integers to XML documents and ByteArray streams, but on the other hand we have the FMS with a JavaScript host. Where is the bridge? 🙂 I mean how does one work with the objects, in particular, a ByteArray, received through the [generic] wire? It's easy with numbers and strings, but no word on more complex objects.

    This topic has been closed for replies.

    1 reply

    Adobe Employee
    June 9, 2010

    Hi,

    I am trying to create a ByteArray object and then receive it through FMS Server in the below code :

    Client side AS3 code

    import flash.utils.ByteArray;

    var nc:NetConnection;
    var ns:NetStream;
    var client:Object;

    var groceries:Array = ["milk", 4.50, "soup", 1.79, "eggs", 3.19, "bread" , 2.35]
    var bytes:ByteArray = new ByteArray();

    for (var i:int = 0; i < groceries.length; i++) {
            bytes.writeUTFBytes(groceries[i++]);
            bytes.writeFloat(groceries);      
    }

    nc = new NetConnection();
    nc.connect("rtmp://localhost/test");
    nc.addEventListener(NetStatusEvent.NET_STATUS, ncStatus);
    function ncStatus(event:NetStatusEvent):void{
    trace("NetConnection event.info.code: " + event.info.code);
    if(event.info.code=="NetConnection.Connect.Success"){
      nc.call("BytesReceived", null, bytes);
      client = new Object();
      nc.client = client;
            client.onBytesReceived = onBytesReceivedHandler;
    }
    }

    function onBytesReceivedHandler(info:Object):void
    {
    if(info.code=="StartReading"){
      bytes.position = 0;
      trace("Item 1 : " + (info.data.readUTFBytes(4)) + " Rate : " + (info.data.readFloat())); 
      trace("Item2 : " + (info.data.readUTFBytes(4)) + " Rate : " + (info.data.readFloat()));

    }
    }

    Server side code

    var obj = new Object();
    application.onConnect = function(client)
    {
    trace("Client Connected...");
    this.acceptConnection(client);
    };

    Client.prototype.BytesReceived = function( bytes )
    {
    if(bytes == undefined)
    {
      trace("BytesReceived for null bytes");
      return;
    }
    obj = bytes;
    this.call("onBytesReceived", null, {code:"StartReading", data:obj});
    }

    Hope this helps else it would be better to clarify your problem a bit more to nail it down on my side.

    Regards,

    Amit

    amnAuthor
    Known Participant
    June 9, 2010

    Well, like I said, I want to know whether and how one can work with a ByteArray object at server side? What you did in the example above is avoid working with it at server-side - you just receive it and send it back to the client, which demonstrates all the working with it. I am aware that FMS can deflate and inflate foreign objects, even ByteArray, because it supports AMF3, i.e. it can receive and send these objects from and to clients in a transparent manner, but one thing I suspect it cannot do is to work with ByteArray objects after it receives them on the wire. But there is no word on this in documentation, either ActionScript 3 or FMS 3.

    Inspiring
    December 6, 2013

    I realize I am years late on this, but I have just figured this out.

    processByteArray = function(byteArrayAsPlainObject){

         // byteArrayAsPlainObject is an object serialized from a ByteArray but without functions

         // we need to get a ByteArray that we can call functions on

         var usableByteArray = new ByteArray();

         byteArrayAsPlainObject.position = 0; // make sure we start from the beginning

         ByteArray.prototype.readBytes.call(byteArrayAsPlainObject, usableByteArray);

         // now usableByteArray contains what was in the original argument, but we can actually use it on the server side

    }