some newbie questions about luvit

254 views
Skip to first unread message

丁树凯

unread,
Sep 25, 2012, 9:17:38 AM9/25/12
to lu...@googlegroups.com
hi,

These days I want to find a lua socket server as my next game's server.
luvit is the one I find that is highly active.
I think you guys are doing wonderful jobs.

I am new to lua and luvit, so my questions may sound naive...

1. how can i do it in luvit like soket.io:
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

I saw the socket echo server under examples/, but I dont know how to do it as the code above.
I wish there were more doc about luvit ..

2. How to handle with connection broken and reconnect?
mobile games' networks are not as stable as PC.
connection broken and reconnect are common. They need taken care.
but I don't know how to do it.

3. Does it use epoll under linux?
My lua server will run under linux. I hope it has high performance in net io.
AFAIK, epoll is the best choise under linux to implement async io.
I don't know what lib luvit uses.

Thank you!  you guys rock!








Tim Caswell

unread,
Sep 25, 2012, 10:28:53 AM9/25/12
to lu...@googlegroups.com
On Tue, Sep 25, 2012 at 8:17 AM, 丁树凯 <dings...@gmail.com> wrote:
> hi,
>
> These days I want to find a lua socket server as my next game's server.
> luvit is the one I find that is highly active.
> I think you guys are doing wonderful jobs.

Thanks!

>
> I am new to lua and luvit, so my questions may sound naive...
>
> 1. how can i do it in luvit like soket.io:
>
> var io = require('socket.io').listen(80);
> io.sockets.on('connection', function (socket) {
> socket.emit('news', { hello: 'world' });
> socket.on('my other event', function (data) {
> console.log(data);
> });
> });
>
>
> I saw the socket echo server under examples/, but I dont know how to do it
> as the code above.
> I wish there were more doc about luvit ..

Sorry there aren't more docs. Basically the luvit APIs in core match
what's in Node.JS core. The docs at nodejs.org will get you pretty
far. I hope to find time to fix this soon.

As far as socket.io, realize that socket.io is a high-level
abstraction over multiple transports (including websockets). The
luvit net module is for basic TCP sockets which are much lower level.

Do you need a duplex (web)socket with the browser, or are just looking
for API examples for a TCP socket?

>
> 2. How to handle with connection broken and reconnect?
> mobile games' networks are not as stable as PC.
> connection broken and reconnect are common. They need taken care.
> but I don't know how to do it.

Yes, I believe there are events emitted when the connection dies. The
unit tests should have some examples, I haven't used the net module
much at all, so I can't speak much for it.

>
> 3. Does it use epoll under linux?
> My lua server will run under linux. I hope it has high performance in net
> io.
> AFAIK, epoll is the best choise under linux to implement async io.
> I don't know what lib luvit uses.
>

Yes, luvit uses libuv which currently uses libev on linux which uses
epoll. libuv uses the best system interface for each platform (IOCP
on windows, kqueue on darwin, epoll on linux...)

> Thank you! you guys rock!

No problem, we've had fun hacking on this. Sorry it's not better
supported. Mostly it's been a hobby of mine and used in production by
a team at Rackspace that already knew both Lua and the Node.JS API.
If anyone else is using luvit seriously, I'd love to hear about it.

-Tim

Tim Caswell

unread,
Sep 25, 2012, 11:33:25 AM9/25/12
to lu...@googlegroups.com
I should mention that I'm playing with a new API style called
continuables. <https://github.com/luvit/continuable/blob/master/continuable.lua>

In this new API, a readable stream is any lua table that has a
:read(callback) method. The callback gets (err, chunk). A writable
stream is any table with a :write(chunk, callback) method.

Making a TCP echo server using the continuable module would look like:

local tcp = require('continuable').tcp

local server = tcp.createServer("0.0.0.0", 3000, function (socket)
local onRead, onWrite
socket:write("Welcome\n")()
onRead = function (err, chunk)
if err then error(err) end
socket:write(chunk)(onWrite)

-- If there was data, then read again
if chunk then
socket:read()(onRead)
end
end
onWrite = function (err)
if err then error(err) end
end
socket:read()(onRead)
end)
print("Echo server listening on port " .. tcp.getsockname(server).port)

Or using coroutines and await sugar:

local tcp = require('continuable').tcp
local newFiber = require('continuable').fiber.new
local await = require('continuable').fiber.await

local server = tcp.createServer("0.0.0.0", 3000, function (socket)
newFiber(function ()
socket:write("Welcome\n")()
repeat
local chunk = await(socket:read())
socket:write(chunk)()
until not chunk
end)(function (err)
error(err)
end)
end)
print("Echo server listening on port " .. tcp.getsockname(server).port)

Vladimir Dronnikov

unread,
Sep 25, 2012, 1:57:07 PM9/25/12
to lu...@googlegroups.com

coro sugar looks really sweet. but it needs to be under heavy load test, really. say, piping a few hundreds of Mb file. i recall new web/static segfaulted such load.

25.09.2012 19:33 пользователь "Tim Caswell" <t...@creationix.com> написал:

Tim Caswell

unread,
Sep 25, 2012, 2:13:12 PM9/25/12
to lu...@googlegroups.com
correction, :read(callback) should be :read()(callback) and
:write(chunk, callback) should be :write(chunk)(callback). It's
correct in the sample code, just not the paragraph above it.
Reply all
Reply to author
Forward
0 new messages