Asynchronous Http

Hello community,

I need my http request to be asynchronous.
From what I’ve gathered, the Corona SDK does not support that in a way that allows me to read the response body and all the response headers.

What I did is write my own Http-Client based on lua sockets. It supports cookies, chunked as well as “unchunked” transfer encoding and connection keep-alive.
Also, it runs asynchronously as a coroutine, meaning that starting an HTTP request is a non blocking action. Once the request is completed, a request-specific callback-function is executed.
It would serve my purpose perfectly. Problem is performance. For unknown reasons, it seems to have much slower response times (time of sending the request to time of having completely received the response) than the synchronous version of the Corona SDK.

Is this just unavoidable, seeing that the SDK-implementation is native while my implementation is Lua? If yes, are there any alternatives, maybe an asynchronous Http-Request that comes with the SDK and provides response body as well as headers that I have overlooked?
If not, any ideas why response times are so slow even though the framerate (frames include the socket.select(…)-call that should tell me immediately if there is data to receive) is high?

Thanks
[import]uid: 107577 topic_id: 19069 reply_id: 319069[/import]

http://developer.anscamobile.com/node/5305 ?

c. [import]uid: 24 topic_id: 19069 reply_id: 73598[/import]

That solution only provides the response body, but not the headers. I however absolutely need the headers as well as the body.

Still, I got my solution running just fine. My first approach was to call my Http:Tick() function through

[lua]Runtime:addEventListener(“enterFrame”, function() Http:tick(); end);[/lua]

Inside Http:tick() I would call mysock:receive() for every socket that has receivable data.

What I found was, that response times were very high (almost 200% compared to synch http), but the time spent within my Http-functions was perfectly okay. So I discovered that the wasted time was actually wasted outside of my code.

[lua]while true do
Http:tick();
end[/lua]

Using this approach I managed to do asynchronous Http just as quick as Corona’s synched Http requests. But of course that is not really a nice approach as it prevents Corona from handling events.
I tested a bit more and finally found, that using the enterFrame event actually works quite perfectly as long as I not only call mysock:receive() once per frame per socket, but as often as there was data to receive.

So at long last I got it working: An asynchronous Http class, with cookies, chunked transfer encoding, socket-pooling (keep-alive) and delivery of response body and headers to a per-request-definable callback.
[import]uid: 107577 topic_id: 19069 reply_id: 73712[/import]