LUA server ON function definition
Posted: Thu Dec 18, 2014 10:43 am
LS,
Learning LUA at this moment and working with the http server example
Updated by me to get the anonymous receive function replaced with one of my own.
Resulted in the code below :
Saved this to "testserver.lua"and run it using dofile("testserver.lua")
But when running I get the following result.
Will be because of my limited knowledge of lua yet but
1/ why is my function pr not recognized as function ? (also tried to define function pr before usage but no difference)
2/ what would be the correct syntax to achieve this ?
Kind regards,
Herman
Learning LUA at this moment and working with the http server example
Code: Select all
-- A simple http server
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
print(payload)
conn:send("<h1> Hello, NodeMcu.</h1>")
end)
conn:on("sent",function(conn) conn:close() end)
end)
Updated by me to get the anonymous receive function replaced with one of my own.
Resulted in the code below :
Code: Select all
if srv ~= nil then srv:close() end
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",pr (conn,payload))
conn:on("sent",function(conn) conn:close() end)
end)
function pr(c,pl)
print (pl)
c:send("Hello from updated")
end
Saved this to "testserver.lua"and run it using dofile("testserver.lua")
But when running I get the following result.
Code: Select all
> SENT: dofile("testserver.lua")
dofile("testserver.lua")
> nil
PANIC: unprotected error in call to Lua API (testserver.lus:4: bad argument #2 to 'on' (function or lightfunction expected, got no value))
?!???1?)M?1?)M?1??
NodeMcu 0.9.2 build 20141212 powered by Lua 5.1.4
lua: cannot open init.lua
>
Will be because of my limited knowledge of lua yet but
1/ why is my function pr not recognized as function ? (also tried to define function pr before usage but no difference)
2/ what would be the correct syntax to achieve this ?
Kind regards,
Herman