Fixes serving multiple connections simultaneously by reopening and scanning the file

This commit is contained in:
Marcos Kirsch 2015-03-15 22:12:00 -05:00
parent be08c78e8b
commit 4f6cd02b1f

View File

@ -14,11 +14,14 @@ end
return function (connection, args)
sendHeader(connection, 200, "OK", getMimeType(args.ext))
print("Begin sending:", args.file)
--print("Begin sending:", args.file)
-- Send file in little chunks
local continue = true
local bytesSent = 0
while continue do
-- NodeMCU file API lets you open 1 file at a time.
-- So we need to open, seek, close each time in order
-- to support multiple simultaneous clients.
file.open(args.file)
file.seek("set", bytesSent)
local chunk = file.read(512)
@ -26,13 +29,11 @@ return function (connection, args)
if chunk == nil then
continue = false
else
if #chunk == 512 then
coroutine.yield()
end
coroutine.yield()
connection:send(chunk)
bytesSent = bytesSent + #chunk
print("Sent" .. args.file, bytesSent)
--print("Sent" .. args.file, bytesSent)
end
end
print("Finished sending:", args.file)
--print("Finished sending:", args.file)
end