Only one request at a time

This commit is contained in:
ollo 2019-04-03 20:23:07 +02:00
parent 7052c4c25a
commit 784a75241c

View File

@ -1,7 +1,7 @@
--TODO: --TODO:
configFile="config.lua" configFile="config.lua"
httpSending=false
sentBytes=0 sentBytes=0
function sendPage(conn, nameOfFile, replaceMap) function sendPage(conn, nameOfFile, replaceMap)
collectgarbage() collectgarbage()
@ -11,8 +11,9 @@ function sendPage(conn, nameOfFile, replaceMap)
conn:close() conn:close()
print("Page sent") print("Page sent")
collectgarbage() collectgarbage()
httpSending=false
else else
print("Next") collectgarbage()
sendPage(conn, nameOfFile, replaceMap) sendPage(conn, nameOfFile, replaceMap)
end end
end) end)
@ -32,7 +33,6 @@ function sendPage(conn, nameOfFile, replaceMap)
local line = file.readline() local line = file.readline()
while (line ~= nil) do while (line ~= nil) do
-- all placeholder begin with a $, so search for it in the current line -- all placeholder begin with a $, so search for it in the current line
if (line:find("$") ~= nil) then if (line:find("$") ~= nil) then
-- Replace the placeholder with the dynamic content -- Replace the placeholder with the dynamic content
@ -43,6 +43,8 @@ function sendPage(conn, nameOfFile, replaceMap)
end end
end end
end end
-- increase the amount of sent bytes -- increase the amount of sent bytes
sentBytes=sentBytes+string.len(line) sentBytes=sentBytes+string.len(line)
@ -126,8 +128,14 @@ function startWebServer()
srv=net.createServer(net.TCP) srv=net.createServer(net.TCP)
srv:listen(80,function(conn) srv:listen(80,function(conn)
conn:on("receive", function(conn,payload) conn:on("receive", function(conn,payload)
if (httpSending) then
print("HTTP sending... be patient!")
return
end
if (payload:find("GET /") ~= nil) then if (payload:find("GET /") ~= nil) then
httpSending=true
--here is code for handling http request from a web-browser --here is code for handling http request from a web-browser
collectgarbage() collectgarbage()
@ -167,8 +175,8 @@ function startWebServer()
file.remove(configFile .. ".new") file.remove(configFile .. ".new")
sec, _ = rtctime.get() sec, _ = rtctime.get()
file.open(configFile.. ".new", "w+") file.open(configFile.. ".new", "w+")
file.write("-- Config\n" .. "station_cfg={}\nstation_cfg.ssid=\"" .. _POST.ssid .. "\"\nstation_cfg.pwd=\"" .. _POST.password .. "\"\nstation_cfg.save=false\nwifi.sta.config(station_cfg)\n") file.write("-- Config\n" .. "station_cfg={}\nstation_cfg.ssid=\"" .. _POST.ssid .. "\"\nstation_cfg.pwd=\"" .. _POST.password .. "\"\nstation_cfg.save=false\nwifi.sta.config(station_cfg)\n")
file.write("sntpserverhostname=\"" .. _POST.sntpserver .. "\"\n" .. "timezoneoffset=\"" .. _POST.timezoneoffset .. "\"\n".. "inv46=\"" .. tostring(_POST.inv46) .. "\"\n") file.write("sntpserverhostname=\"" .. _POST.sntpserver .. "\"\n" .. "timezoneoffset=\"" .. _POST.timezoneoffset .. "\"\n".. "inv46=\"" .. tostring(_POST.inv46) .. "\"\n")
if ( _POST.fcolor ~= nil) then if ( _POST.fcolor ~= nil) then
-- color=string.char(_POST.green, _POST.red, _POST.blue) -- color=string.char(_POST.green, _POST.red, _POST.blue)
@ -297,3 +305,4 @@ function startWebServer()
end) end)
end end
--FileView done.