Getting all response bytes onComplete

13 views
Skip to first unread message

mr

unread,
Nov 10, 2008, 1:58:31 PM11/10/08
to AS3 HttpClient, ma...@ruggier.org
Hi,

iiuc, an HttpDataEvent is fired for each received chunk of data, and
each of those events has a ByteArray attribute that is set to the
bytes received in that chunck. And, onComplete will fire an
HttpResponseEvent, but that event instance does not contain a way to
get to the response complete response data. Is that correct?

This implies that for big-enough responses one has to manage each
httpData event, and take care to concat the sum-total bytearray for
the response. This is great that one can do this, per chunck... but in
the vast majority of cases you just want the total response data, as
one object.

I suspect I may have misunderstood something here... while it is not
such a big deal to intercept each httpResponseData event and build up
the bytes array, and than make that avaliable also on an
httpResponseComplete event (i have done exactly this and it is working
fine) is this the intended use of this api?

mario

gabe

unread,
Nov 10, 2008, 6:09:16 PM11/10/08
to AS3 HttpClient
Yeah... The API pushes the developer to handle bytes themselves. For
example, downloading a huge file, the http client wouldn't want to
keep the full response in memory (its memory usage would explode).
Rather it makes sense to write the bytes to disk as they are streamed
in.

But as you point out, there is a common use case for wanting to deal
with all the bytes at the end (for small responses). I think I could
add a mode where you can tell the client to keep track of the bytes
and have it available at the end for this use case.

Thanks for the feedback!

gabe

unread,
Dec 2, 2008, 3:38:07 PM12/2/08
to AS3 HttpClient
I finally got around to adding the onDataComplete listener. I updated
the examples page:

To get notified of all data when the response data has been received,
use the HttpDataListener? with HttpDataListener?#onDataComplete.

var client:HttpClient = new HttpClient();
var listener:HttpDataListener = new HttpDataListener();

listener.onDataComplete = function(event:HttpResponseEvent,
data:ByteArray):void {
// Notified with response content
};

var uri:URI = new URI("http://www.nytimes.com");
client.get(uri, listener);

As a side effect listeners can be added on a per-request basis, which
makes more sense in general.. The HttpClient#listener was a default
that is nice to have, although makes things a bit more complex.
Reply all
Reply to author
Forward
0 new messages