As the title says... Chat on...

User avatar
By hreintke
#5079 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
User avatar
By hreintke
#5161 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
User avatar
By fsankar
#66359
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.