luvit/luvit — pull request #65 by rphillips (2012-01-06 05:04:12+00:00)
Title: Net module

* Functional
* Add utils.inherits
* Add listening event

tcp client becomes:

local net = require('net')

local client
client = net.createConnection(8080, '127.0.0.1', function(err)
if err then
p(err)
return
end

print("Connected...")

process.stdin:read_start()

process.stdin:pipe(client)
client:pipe(process.stdout)
end)

TCP Server becomes:

local net = require('net')

local server = net.createServer(function (client)
p("on_connection", client)

print("Adding listener for data events")
client:on("data", function (chunk)
p("on_read", chunk)

print("Sending chunk back to client")
client:write(chunk, function (err)
p("on_written", err)
end)

end)

print("Adding listener for close event")
client:on("end", function ()
p("on_end")

print("Closing connection")
client:close(function ()
p("on_closed")
end)
end)

end)

server:listen(8080, '0.0.0.0', function(err)
print("TCP echo server listening on port 8080")
end)

print("Listening for errors in the server")
server:on("error", function (err)
p("ERROR", err)
end)