AS3,AIR,插座。如何检查writeObject()传输的进度?

时间:2013-11-27 19:49:06

标签: actionscript-3 flash sockets air

我正在处理AIR和套接字,使用服务器应用程序和另一个作为客户端。

服务器向Client发送一个对象:

clientSocket.writeObject(myObject);

客户端有一个监听器,如下所示:

socket.addEventListener(ProgressEvent.SOCKET_DATA, socketData);

如何知道传入对象的大小?我需要监控这个过程,因为当传输完成时我需要做另一个过程。

我尝试了这个,但不起作用:

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);
}

}

这篇文章在我的具体案例中不起作用:AS3 / AIR readObject() from socket - How do you check all data has been received?

1 个答案:

答案 0 :(得分:0)

我用黑客解决它:

SERVER:发送一个对象:

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

客户:

一个。让侦听器接收套接字数据:

socket.addEventListener(ProgressEvent.SOCKET_DATA, socketData);

B中。接收字节。当所有字节都可用时,请读取对象:

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;

}