Post your best Lua script examples here

User avatar
By roccomuso
#23333 Hi guys,

following this code:
Code: Select all    -- create a server
    sv=net.createServer(net.TCP, 30)    -- 30s time out for a inactive client
    -- server listen on 80, if data received, print data to console, and send "hello world" to remote.
    sv:listen(80,function(c)
      c:on("receive", function(c, pl) print(pl) end)
      c:send("hello world")
      end)


how may i send a reply to the client setting the headers?
User avatar
By TerryE
#23526 Read the HTTP RFCs and the Wikipedia article on this, plus the many examples out on web. If you use Ff or Chrome, there is an inbuilt diagnostic mode so you can analyse these exchanges between a client and an HTTP server. Essentially the client sends a bunch of HTTP headers following the GET or POST request (in the same record in the case of HTTP 1.1 which the server must parse (in your case the content of the pl field, and then having processed them your app must reply with its own headers and optional content.

All straight forward with a little bit of reading up on the material out there on the internet.
User avatar
By TerryE
#23659 There's quite a few examples in the lua code included in the distro. But don't do two sends as queuing these up will burn RAM. Better:
Code: Select allc:send(table.concat ({
    "HTTP/1.1 200 OK",
    "Content-Type: application/json",
    "Content-length: " .. #body,
    "",
    body
  }, "\r\n"))

Note that to be HTTP 1.1 compliant, you either need to explicitly specify a content length or transfer encoding or use a 204 return (no content).