0

I'm dealing with AIR and Sockets, using a Server app and another as Client.

Server sends an object to Client:

clientSocket.writeObject(myObject);

Client has a listener, like this:

socket.addEventListener(ProgressEvent.SOCKET_DATA, socketData);

How can I know the size of the incoming object? I need to monitoring this process, cause when the transfer is complete I need to do another processes.

I tried this, but doesn't work :

var total:int = 0;
private function socketData(e:ProgressEvent) :void {


if (total == 0) {
    total = socket.readInt();
}

if (socket.bytesAvailable >= total) {

    trace('COMPLETE');

    total = 0;

} else {

    trace('progress:' + socket.bytesAvailable + ' | total: ' + total);
}

}

This post does not work in my specific case: AS3 / AIR readObject() from socket - How do you check all data has been received?

Community
  • 1
  • 1
Miguel Lara
  • 183
  • 1
  • 12

1 Answers1

0

I resolve it, with a hack:

SERVER: Send an object:

clientSocket.writeObject(image);// var image:Bitmap
clientSocket.flush();

CLIENT:

A. Have a listener to receive socket data:

socket.addEventListener(ProgressEvent.SOCKET_DATA, socketData);

B. Receive the bytes. When all the bytes are available, then read the object:

private var prevBytes:int = -1;

private var currentBytes:int = 0;

private function onEnterFrame(e:Event):void {

    if (currentBytes == prevBytes) {

        removeEventListener(Event.ENTER_FRAME, onEnterFrame);
        currentBytes = 0;
        prevBytes = -1;

        var obj:* = socket.readObject();


    } else {

        prevBytes = currentBytes;

    }

    trace('Current : ' + currentBytes + ' | Prev : ' + prevBytes);

}

private function socketData(e:ProgressEvent):void {

    trace('on socketData : ' + currentBytes);

    if (currentBytes == 0) {
        addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }

    currentBytes = socket.bytesAvailable;

}
Miguel Lara
  • 183
  • 1
  • 12