我正在测试一个具有自动化功能的LUA Web服务器。HTML代码每秒都将其重定向到Web服务器本身。因此,客户端的Web浏览器总是从服务器获取新数据,而不是使用浏览器的缓存。
如果我在一段时间后甚至只与一个客户端(我的PC或智能手机)连接,则NodeMcu-Board崩溃了此消息:
恐慌:呼叫lua api的未保护错误(so-websrv-test.lua:27:不记忆)
我从Marcel Stoer那里使用了此代码,他在类似的"记忆中耗尽"问题上回答了。
我修改了Marcel的LUA代码,但是此代码随着时间的流逝仍然会吞噬所有堆的内存。
我稍微缩小了问题:如果HTML代码的刷新频率低于30秒,则代码吞噬了堆内存。
那么我必须如何修改此代码才能实现恒定的堆内内存使用?
最好的问候。
Stefan
tmr.alarm(0, 1000, 1, function()
if wifi.sta.getip() == nil then
print("trying to connect to AccessPoint...")
else
own_ip, netmask, gateway=wifi.sta.getip()
print("connected to AccessPoint:")
print("IP Info: nIP Address of this device: ",own_ip)
print("Netmask: ",netmask)
print("Gateway Addr: ",gateway,"n")
print("type IP-Address "..own_ip.." into your browser to display SHT-31-website")
tmr.stop(0)
end
end)
counter = 0
srv = net.createServer(net.TCP, 28800)
print("Server created... n")
srv:listen(80, function(conn)
conn:on("receive", function(sck, request)
local message = {}
counter = counter + 1
message[#message + 1] = "<head> <meta http-equiv=refresh content=1; URL=http://"..own_ip.."> </head>"
message[#message + 1] = "<h1> ESP8266 SHT-31 Web Server Ver 003</h1>"
message[#message + 1] = "<h2>some more text blabla blub"..counter.."</h2>"
local function send(sk)
if #message > 0 then
sk:send(table.remove(message, 1))
else
sk:close()
message = nil
print("Heap Available:" .. node.heap())
end
end
sck:on("sent", send)
send(sck)
end)
end)
您没有告诉我们您使用的固件版本。我在Chromium浏览器中测试了最近的版本,但没有看到任何内存问题。我在700多次重新加载周期后流产了测试,堆消耗绝对稳定。
今年早些时候,我们不得不减少TCP TIME_WAIT
参数值,因为在时间等状态下保存的废弃插座正在吞噬记忆。说明:
时间等待
(服务器或客户端)表示等待足够的时间通过以确保远程TCP收到了其确认连接终止请求。[根据RFC 793,连接可以在时间等待中最多四分钟称为两个MSL(最大段寿命)。]
来源:https://en.wikipedia.org/wiki/transmission_control_protocol#protocol_operation
更多详细信息:https://www.rfc-editor.org/rfc/rfc7230#section-6.6
但是:
- 您似乎打算将HTML通过HTTP发送回您的客户,但不要告诉您,它可能不喜欢
- 如果您的客户端(浏览器?)不会按时关闭旧插座,您也可以告诉它明确地这样做
添加适当的HTTP标头均已修复。因此,消息部分应该是这样:
local message = { "HTTP/1.0 200 OKrnServer: NodeMCU on ESP8266rnConnection: closernContent-Type: text/htmlrnrn" }
counter = counter + 1
message[#message + 1] = "<html><head> <meta http-equiv=refresh content=1; URL=http://" .. own_ip .. "> </head>"
message[#message + 1] = "<body><h1> ESP8266 SHT-31 Web Server Ver 003</h1>"
message[#message + 1] = "<h2>some more text blabla blub" .. counter .. "</h2></body></html>"
请注意Connection: close
和Content-Type: text/html
以及正确结构的HTML标记。