如何在Lua中使用回调进行异步HTTP调用? [英] How to make asynchronous HTTP call with callback in Lua?

查看:1050
本文介绍了如何在Lua中使用回调进行异步HTTP调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对服务器进行异步HTTP调用才能接收XML响应. 得到响应后,如果成功则将调用[先前指定的]函数,如果出错则将调用其他函数.

I need to make asynchronous HTTP call to my server in order to receive XML response. After I get the response I will call a [previously specified] function if it is success or some other function if it's an error.

所以我首先想到的是协程.不幸的是,在我调用http.get之后,我无法让步,因为它将等待整个过程完成.我知道我可以使用单独的函数来读取响应,但是我必须至少等待数据的第一个字节才能触发此函数,这将使我屈服.无论如何,因为我想使用协程做的事情看起来并不可行.

So what I thought about in the first place was coroutines. Unfortunately, after I make the http.get call I cannot yield, as it will wait for the whole thing to finish. I know I can use separate functions to read the response, however I have to wait at least for the first bytes of data in order for this function to be triggered which would allow me to yield. Anyway, for what I wan to do using coroutines doesn't look like the way to go.

然后,我尝试从lua调用C函数,创建单独的线程以获取XML,然后在Lua中调用函数,但是由于创建新线程后lua_state发生了更改,因此这不起作用.在创建线程之前,我可以在堆栈上看到3个参数,在创建新线程之后[我将lua_State作为参数传递],它只有一个.无论如何,据我了解,一旦原始的函数调用完成,lua_State将关闭,因此我将无法回叫.

Then I've tried calling a C function from lua, creating separate thread to get the XML and then call a function in Lua, however this doesn't work because of lua_state changing after a new thread is created. Before the thread is created I can see 3 parameters on the stack, and after creation of the new thread [I am passing lua_State as the argument] it has only one. Anyway, from what I understand lua_State will be closed once the original cfunction call is finished, so I won't be able to call back.

由于我只是从lua入手,而对lua到c的绑定我甚至还不那么熟悉,所以我只能希望自己会犯一些愚蠢的错误,并且很容易解决.但是,到目前为止,我对如何进一步发展一无所知.

Since I'm just starting with lua and I'm even less familiar with lua to c bindings I can only hope I'm making some stupid mistakes and it will be easy to solve. For now however I'm stuck with no idea on how to progress further.

此问题背后的故事: 我正在将游戏从Cocos2D目标C框架移植到Cocos2d-X C ++框架.我想使用Lua绑定,因为我认为我将无法将其移植到C ++.无论如何,我想在Lua中做到这一点. 所以我有一个场景,有人可以访问他们在游戏中拥有的库存清单.如果立即做出响应,他们将基本上看到一个打开的窗口,其中包含库存清单.但是,如果花一点点时间才能获取数据[连接问题,严重过载...等等]屏幕,则该屏幕将消失,并且一些动画指示数据传输将显示在屏幕上.至少这是它在游戏的objc版本上的工作方式,我想要相同的东西.

The story behind this issue: I'm porting my game from Cocos2D objective C framework to Cocos2d-X C++ framework. I want to use Lua bindings as I think I will fail to port it to C++. Anyway I want to do it in Lua. So I've got a scene where someone accesses a list of inventory they have in the game. If the response is immediate they will basically see a window opened with list of inventory. However, if it takes a tad bit longer to get the data [connection issues, sever overload... whatever] screen will fade out and some animation indicating data transfer will be shown on screen. At least this is how it works on the objc version of the game and I want the same thing.

在研究过程中有什么我想念的东西吗?

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

顺便说一句,我见过 Lua套接字异步调用,它对我没有帮助,因为它仍然等待传输的开始,然后才能开始另一传输.

BTW I have seen Lua socket asynchronous calls and it doesn't help me because it still waits for the beginning of the transfer before it will start another one.

推荐答案

我能够使用 https来做到这一点://github.com/Neopallium/lua-llthreads 这似乎在iOS和Android平台上都能正常工作.

I was able to do it using https://github.com/Neopallium/lua-llthreads This seems to work fine on both iOS and Android platforms.

这篇关于如何在Lua中使用回调进行异步HTTP调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆