How to make an asynchronous http callback in Lua?

I need to make an asynchronous http call on my server in order to get an XML response. After I get the answer, I will call the function [previously specified] if it is success or some other function if it is an error.

So, what I was thinking about, first of all, were coroutines. Unfortunately, after I make an http.get call, I cannot give in, as he will wait for the completion of everything. I know that I can use separate functions to read the response, however I need to wait, at least for the first bytes of data, for this function to be launched, which would allow me to give way. Anyway, for what I wanted to do using coroutines, it doesn't seem to go.

Then I tried to call the C function from lua, creating a separate stream to get the XML, and then calling the function in Lua, however this does not work due to the lua_state change after creating a new stream. Before creating a stream, I can see 3 parameters on the stack, and after creating a new stream [I pass lua_State as an argument] it has only one. In any case, from what I understand, lua_State will be closed after the cfunction function call is completed, so I won’t be able to call back.

Since I'm just starting out with lua, and I'm even less familiar with lua bindings to c, I can only hope that I will make stupid mistakes and this will be easy to solve. In the meantime, I do not think about how to move forward.

History of this problem: I am transferring my game from the C-COS2D object C-frame to Cocos2d-X C ++. I want to use Lua bindings as I think that it will not be possible to port it to C ++. Anyway, I want to do it in Lua. So, I have a scene where someone gets a list of the inventory that they have in the game. If the answer is immediate, they will basically open a window opened by the inventory list. However, if it takes a little longer to get the data, connection problems, turn off the overload ... no matter which screen disappears, and an animation showing the data transfer will be displayed on the screen. At least that's how it works on the objc version of the game, and I want the same thing.

Is there something that I missed during my research, is it possible to do this?

BTW Lua, , , .

+2
2
+1

- Luvit?

Luvit - - , nodeJS awesome , Lua .

, . hello world 2 4 , nodeJS.

+5

All Articles