prepared sending of one file in multiple parts

This commit is contained in:
ollo 2016-12-21 23:56:37 +01:00
parent 4b1cea0224
commit 7f0d578ca5

View File

@ -2,14 +2,13 @@
configFile="config.lua" configFile="config.lua"
sentLines=0 sentBytes=0
sendFile=nil sendFile=nil
function sendPage(conn, nameOfFile) function sendPage(conn, nameOfFile)
if (sentLines > 0) then if (sentBytes <= 0) then
print("There is a site already sent") print("There is a site already sent")
return sendFile=nameOfFile
end end
sendFile=nameOfFile
if file.open(sendFile, "r") then if file.open(sendFile, "r") then
local line = file.readLine() local line = file.readLine()
@ -17,11 +16,13 @@ function sendPage(conn, nameOfFile)
local buf="" local buf=""
while (line ~= nil) do while (line ~= nil) do
buf = buf .. line buf = buf .. line
sentLines= sentLines+1 sentBytes=sentBytes+string.len(line)
-- Sent after 1k data -- Sent after 1k data
if (string.len(buf) >= 1000) then if (string.len(buf) >= 1000) then
line=nil line=nil
conn.send(buf) conn.send(buf)
-- end the function, this part is sent
return
else else
-- fetch the next line -- fetch the next line
line = file.readLine() line = file.readLine()
@ -30,10 +31,14 @@ function sendPage(conn, nameOfFile)
end end
conn:on("sent", function(conn) conn:on("sent", function(conn)
conn:close() if (sentBytes == 0) then
-- Clear the webpage generation conn:close()
sendWebPage=nil -- Clear the webpage generation
print("Clean webpage from RAM") sendWebPage=nil
print("Clean webpage from RAM")
else
end
end) end)
end end