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

User avatar
By kamal sonani
#53475 i use
Code: Select all   
GPIO0  = 3
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
   conn:on("receive",function(conn,payload)
   if gpio.read(GPIO0)==1
      then sw="off"
      else sw="on"
   end
   conn:send("<h1>The switch is " .. sw ..".</h1>")
   end)
   conn:on("sent",function(conn)
      conn:close()
   end)
end)


whenever any single user change the status of pin , all other users page might be update. :roll:
however i set my browser to continues refresh and display the status of device (GPIO) in users app
like " The switch is On __ Off "

what about ajax OR web-socket? any idea :?:
User avatar
By marcelstoer
#53482 https://github.com/creationix/nodemcu-webide has a websocket server included: https://github.com/creationix/nodemcu-w ... socket.lua

However, 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'.