nodemcu写入HTML,在Nodemcu Esp8266 lua编程中使用client:send()发送整个html代码

其他海报提供了很多很好的信息,但我认为即使在非正式的常见问题解答中,也没有给出这个问题的特定解决方案。

要发送大型静态文件,您可以使用回调从闪存中加载它们并以块的形式发送它们。正如其他人所提到的,一次通话可以发送多少是有限的。单个回调中的多个发送调用可能无法按预期处理,并且可能占用太多内存。这是一个以块的形式加载和发送文件的简短示例:

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值