0

我可以使用这些代码轻松地将 bytearray 发送到服务器:

var req:URLRequest = new URLRequest(_var1);
req.contentType = 'application/octet-stream';
req.method = URLRequestMethod.POST;
req.data = DATAS;   
var loader:URLLoader = new URLLoader(req);

在这个数据中, DATAS 是一个字节数组,它已经准备好了,但我想发送实时生成的字节数组,实际上没有长度。

我想流式传输字节数组,我该怎么做?

4

1 回答 1

1

您需要套接字连接:

    var socket:Socket = new Socket();
    socket.connect(host, port);
    socket.addEventListener(Event.CONNECT, function(event:Event):void
    {
        socket.writeBytes(DATAS);
    });

用于注释的测试代码的 UPD:

    var _buffer:ByteArray = new ByteArray();
    _buffer.writeByte(0xFF);
    trace("_buffer=",ByteArrayUtil.getHexBytesString(_buffer));

    var ou:ByteArray = new ByteArray();
    ou.writeBytes(_buffer , 0 ,_buffer.length);
    _buffer.clear();        

    trace("ou=\t", ByteArrayUtil.getHexBytesString(ou));
    trace("_buffer=", ByteArrayUtil.getHexBytesString(_buffer));


public static function getHexBytesString(array:ByteArray, colons:Boolean=false, position:int = 0, len:int = 0):String {
    var s:String = "";
    if(!array)
        return s;

    if(len)
    {
        len += position;
        len = Math.min(len, array.length);
    }
    else
        len = array.length;

    for (var i:int=position;i<len;i++) {
        s+=("0"+array[i].toString(16)).substr(-2,2).toUpperCase();
        if (colons && i<len-1) s+= "-";
    }
    return s;
}

输出:

_buffer= FF
ou=  FF
_buffer= 
于 2013-09-20T07:32:09.173 回答