-->
Page 1 of 2

LUA server ON function definition

PostPosted: Thu Dec 18, 2014 10:43 am
by hreintke
LS,
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 allif 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

Re: LUA server ON function definition

PostPosted: Thu Dec 18, 2014 10:54 am
by ThomasW
Hi,

The second parameter for socket:on() must be a function,
but with:
Code: Select allconn:on("receive",pr (conn,payload))

you're passing the *result* of pr(conn,paylod).
Change to:
Code: Select allconn:on("receive",pr)

Thomas

Re: LUA server ON function definition

PostPosted: Fri Dec 19, 2014 8:56 am
by hreintke
Thomas,

Thanks, that was indeed the mistake I made.
Learning Lua is speeding up now.

I presume the "Panic unprotected error" and reboot is a bug in the LUA interpreter.

Will submit a separate post on that in the appropriate subforum.

Herman

Re: LUA server ON function definition

PostPosted: Fri May 26, 2017 12:09 pm
by fsankar
hreintke wrote:Thomas,

Thanks, that was indeed the mistake I made.
Learning Lua is speeding up now.

I presume the "Panic unprotected error" and reboot is a bug in the LUA interpreter.

Will submit a separate post on that in the appropriate subforum.

Herman


Herman or Thomas or anyone,
Where did conn come from? sorry I am new to the software. what is conn . where can I find how to use it?
Thanks for any reply.