- Sun Aug 21, 2016 8:14 am
#53482
https://github.com/creationix/nodemcu-webide has a websocket server included:
https://github.com/creationix/nodemcu-w ... socket.luaHowever, I doubt you really need that, AJAX will work fine just as well. Not sure if you're familiar with AJAX in general but if not I suggest you take a look at
http://www.tutorialspoint.com/jquery/jquery-ajax.htm.
Your "server" would simply return 'on' or 'off' rather than an HTML fragment like you do now. That return value will then be injected into your HTML page in the AJAX success callback handler.
Btw, the code you posted is sooner or later going to cause out-of-memory errors on the ESP8266. The reason for that is that you're re-using the 'conn' variable in the callback functions. See
http://stackoverflow.com/a/37379426/131929 for details. What you want to do is something like this instead as documented with
net.socket:send().
Code: Select allsrv:listen(80,function(conn)
conn:on("receive",function(sck,payload)
..
sck:send("<h1>The switch is " .. sw ..".</h1>")
end)
conn:on("sent",function(sck)
sck:close()
end)
end) See how the inner callbacks use 'sck' rather than 'conn'.