Send the answer 5 milliseconds after writing the file

This commit is contained in:
ollo 2016-12-30 15:41:19 +01:00
parent 38e591d061
commit 1bbfb7feee

View File

@ -4,12 +4,13 @@ configFile="config.lua"
sentBytes=0 sentBytes=0
function sendPage(conn, nameOfFile, replaceMap) function sendPage(conn, nameOfFile, replaceMap)
print("Sending " .. nameOfFile .. " " .. sentBytes .. "B already")
conn:on("sent", function(conn) conn:on("sent", function(conn)
if (sentBytes == 0) then if (sentBytes == 0) then
conn:close() conn:close()
print("Page sent") print("Page sent")
else else
print("Next")
sendPage(conn, nameOfFile, replaceMap) sendPage(conn, nameOfFile, replaceMap)
end end
end) end)
@ -48,6 +49,7 @@ function sendPage(conn, nameOfFile, replaceMap)
if (string.len(buf) >= 700) then if (string.len(buf) >= 700) then
line=nil line=nil
conn:send(buf) conn:send(buf)
print("Sent part of " .. sentBytes .. "B")
-- end the function, this part is sent -- end the function, this part is sent
return return
else else
@ -59,6 +61,7 @@ function sendPage(conn, nameOfFile, replaceMap)
sentBytes=0 sentBytes=0
-- send the rest -- send the rest
conn:send(buf) conn:send(buf)
print("Sent rest")
end end
end end
@ -209,14 +212,19 @@ function startWebServer()
print("Rename config") print("Rename config")
if (file.rename(configFile .. ".new", configFile)) then if (file.rename(configFile .. ".new", configFile)) then
print("Successfully") print("Successfully")
dofile(configFile) -- load the new values tmr.alarm(3, 5, 0 ,function()
replaceMap=fillDynamicMap() dofile(configFile) -- load the new values
replaceMap["$ADDITIONAL_LINE"]="<h2><font color=\"green\">New configuration saved</font></h2>" replaceMap=fillDynamicMap()
sendPage(conn, "webpage.html", replaceMap) replaceMap["$ADDITIONAL_LINE"]="<h2><font color=\"green\">New configuration saved</font></h2>"
print("Send success to client")
sendPage(conn, "webpage.html", replaceMap)
end)
else else
replaceMap=fillDynamicMap() tmr.alarm(3, 5, 0 ,function()
replaceMap["$ADDITIONAL_LINE"]="<h2><font color=\"red\">ERROR</font></h2>" replaceMap=fillDynamicMap()
sendPage(conn, "webpage.html", replaceMap) replaceMap["$ADDITIONAL_LINE"]="<h2><font color=\"red\">ERROR</font></h2>"
sendPage(conn, "webpage.html", replaceMap)
end)
end end
else else
replaceMap=fillDynamicMap() replaceMap=fillDynamicMap()
@ -247,6 +255,7 @@ function startWebServer()
end) end)
conn:on("disconnection", function(c) conn:on("disconnection", function(c)
print("Goodbye")
node.output(nil) -- un-register the redirect output function, output goes to serial node.output(nil) -- un-register the redirect output function, output goes to serial
--reset amount of sent bytes, as we reached the end --reset amount of sent bytes, as we reached the end