lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On Wed, Jan 27, 2021 at 3:00 AM 孙世龙 sunshilong <sunshilong369@gmail.com> wrote:
Hi, list

Besides LuaSocket, is there any other library that provides support
for the TCP and UDP transport layers for Lua?

I am using vanilla Lua.

Stable and open source project is preferred.
Any guideline or suggestion is welcome.

Thank you for your attention to my question.

We've been very happy with luv, which provides Lua bindings to the libuv event loop.

If you want an event loop, async, callback-style program, it's an excellent choice.

You can wrap callbacks inside a coroutine, by executing the call to e.g. a TCP fetch, yielding,
then resuming the coroutine inside the callback, like this (example from Tim Caswell):

local function sleep(ms, answer)
  local co = coroutine.running()
  local timer = uv.new_timer()
  timer:start(ms, 0, function ()
    timer:close()
    return coroutine.resume(co, answer)
  end)
  return coroutine.yield()
end

-- Using it would look like:

coroutine.wrap(function ()
  print "Getting answer to everything..."
  local answer = sleep(1000, 42)
  print("Answer is", answer)
end)()

which gives a nice imperative style to non-blocking code.

cheers,
-Sam.