他の投稿者は多くの有益な情報を提供してくれましたが、非公式の FAQ でさえも、この問題に対する適切な具体的な解決策がまだ提供されていないと思います。
大きな静的ファイルを送信するには、フラッシュから読み込み、コールバックを使用してチャンクで送信できます。他の方もおっしゃっていますが、1回の通話で送れる量には限りがあります。また、単一のコールバックでの複数の送信呼び出しは、期待どおりに処理されない可能性があり、大量のメモリを消費する可能性があります。ファイルをチャンクでロードして送信する簡単な例を次に示します。
local idx = 0 --keep track of where we are in the file
local fname = "index.html"
function nextChunk(c) --open file, read a chunk, and send it!
file.open(fname)
file.seek("set", idx)
local str = file.read(500)
if not str then return end --no more to send.
c:send(str)
idx = idx + 500
file.close() --close the file to let other callbacks use the filesystem
end
client:on("sent", nextChunk) --every time we send a chunk, start the next one!
nextChunk(client) --send the first chunk.
または、コルーチンとタイマーを使用して、より柔軟にすることができます。
local ready = true --represents whether we are ready to send again
client:on("sent", function() ready=true end) --we are ready after sending the previous chunk is finished.
local function sendFile(fname)
local idx=0 --keep track of where we are in the file
while true do
file.open(fname)
file.seek("set", idx)
local str = file.read(500)
if not str then return end --no more to send.
c:send(str)
ready=false --we have sent something. we are no longer ready.
idx = idx + 500
file.close() --close the file to let other callbacks use the filesystem
coroutine.yield() --give up control to the caller
end
end
local sendThread = coroutine.create(sendFile)
tmr.alarm(0, 100, 1, function() --make a repeating alarm that will send the file chunk-by-chunk
if ready then coroutine.resume(sendThread, "index.html") end
if coroutine.status(sendThread) == "dead" then tmr.stop(0) end --stop when we are done
end)