Post your best Lua script examples here

User avatar
By Gus Smith
#23583 I'm using NodeMCU 0.9.6 build 20150216 and esp-01

Test script:

Code: Select allcnt=0

    sv=net.createServer(net.TCP)   
   
    sv:listen(80,function(c)
      c:on("receive", function(c, pl)
         c:send("<h1>hello world</h1>")
         print("sending hello " .. cnt)
         cnt=cnt+1
      end)
     
      c:on("sent", function(conn)
        conn:close()
      end)
    end)


I open url http://192.168.1.72

In the esplorer terminal I see that the "receive" event is called twice every time and in the console I see:

Code: Select allsending hello 0
sending hello 1


Is this normal behaviour?
User avatar
By kolban
#23587 The symptom you describe would happen if there were TWO browser originated called to your device. I'd check your browser network trace and see if there isn't an explanation as to why there might be two calls from your browser environment.
User avatar
By Gus Smith
#23590 I found it, also after reading this http://stackoverflow.com/questions/11961902/nodejs-http-createserver-seems-to-call-twice

It's browser related, nothing to do with Lua.
I am using Chrome and printed the payload and looked at the requests.

The first one from Chrome is a get to "/"

Code: Select allGET / HTTP/1.1


Th 2nd get is to:

Code: Select allGET /favicon.ico HTTP/1.1


On Iphone 5s/Safari there is only one request to "/"

I will have to parse and ignore the request to /favicon