Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run blocking operation in Corona SDK Lua?

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)

like image 629
ZAN Avatar asked Dec 05 '25 02:12

ZAN


2 Answers

Corona contains LuaSockets which will let you do asynchronous socket communication, as seen here.

like image 131
Mud Avatar answered Dec 07 '25 23:12

Mud


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.

like image 33
SatheeshJM Avatar answered Dec 07 '25 23:12

SatheeshJM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!