I'm new to LUA and I'm writing a tcp messaging library in LUA using Corona SDK. I stuck with a problem that socket reading operation hangs application UI even if it is run in coroutine.
How I start coroutine:
function Messaging:readLoop()
self.readCoroutine = coroutine.create(function() self:blockingLoop() end)
coroutine.resume(self.readCoroutine)
end
Blocking loop:
function Messaging:blockingLoop()
line,err,rest = self.sock:receive(BUFSIZE) -- <= Hangs UI if there is no incoming data
end
Of course I know that coroutines are not equal to threads, but I expected that LUA interpreter switched to another coroutine on blocking operation (like Python threads with GIL). Is there any possibility to read from socket without blocking UI? For example with real threading or async approach? Thanks.
P.S. Eliminating BUFSIZ is not the option since I don't want to have UI blocked at all, even for 0.2..0.4 seconds (slow mobile network delay)
Corona contains LuaSockets which will let you do asynchronous socket communication, as seen here.
Corona has a network.request API for Asynchronous calls..
If you do not want to use that, take a look at this asynchronous http library.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With