如何使用新的 SDK (NodeMCU) 发送多个数据 (conn:send())

2023-12-04

我一直在阅读 NodeMCU 文档和几个有关 SDK 更改的已解决问题,这些 SDK 以前允许发送多个数据流(就像排队的 net.socket:send 一样)。

这里似乎引发了一场巨大的争论(#730)和那里(#993)或者甚至在这里(#999)。然而,我没有找到任何令人信服的网络服务器代码示例,可以让我读取多个 html 文件(例如head.html and body.html) 显示页面。这是我尝试改编的 TerryE 示例,但没有成功:

srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
    conn:on ("receive", function(sck, req)
        local response = {}

        local f = file.open("head.html","r")
        if f ~= nil then
            response[#response+1] = file.read()
            file.close()
        end

        local f = file.open("body.html","r")
        if f ~= nil then
            response[#response+1] = file.read()
            file.close()
        end

        local function sender (sck)
            if #response>0 then sck:send(table.remove(response,1))
            else sck:close()
            end
        end
        sck:on("sent", sender)
        sender(sck)
    end )
end )

连接到 ESP8266 时,没有加载任何内容,并且我从 lua 终端没有收到任何错误。

供你参考,head.html包含:

<html>
<head>
</head>

And body.html包含:

<body>
<h1>Hello World</h1>
</body>
</html>

我对 NodeMCU 很陌生,请宽容。


这是我的解决方案,不使用表,节省了一些内存:

function Sendfile(sck, filename, sentCallback)
    if not file.open(filename, "r") then
        sck:close()
        return
    end
    local function sendChunk()
        local line = file.read(512)
        if line then 
            sck:send(line, sendChunk) 
        else
            file.close()
            collectgarbage()
            if sentCallback then
                sentCallback()
            else
                sck:close()
            end
        end
    end
    sendChunk()
end


srv = net.createServer(net.TCP)
srv:listen(80, function(conn)
    conn:on("receive", function(sck, req)
        sck:send("HTTP/1.1 200 OK\r\n" ..
            "Server: NodeMCU on ESP8266\r\n" ..
            "Content-Type: text/html; charset=UTF-8\r\n\r\n", 
            function()
                Sendfile(sck, "head.html", function() Sendfile(sck, "body.html") end)
            end)        
    end)
end)

这是用于服务单个文件:

function Sendfile(client, filename)
    if file.open(filename, "r") then
        local function sendChunk()
            local line = file.read(512)
            if line then 
                client:send(line, sendChunk) 
            else
                file.close()
                client:close()
                collectgarbage()
            end
        end
        client:send("HTTP/1.1 200 OK\r\n" ..
            "Server: NodeMCU on ESP8266\r\n" ..
            "Content-Type: text/html; charset=UTF-8\r\n\r\n", sendChunk)
    else
        client:send("HTTP/1.0 404 Not Found\r\n\r\nPage not found")
        client:close()
    end
end


srv = net.createServer(net.TCP)
srv:listen(80, function(conn)
    conn:on ("receive", function(client, request)
        local path = string.match(request, "GET /(.+) HTTP")
        if path == "" then path = "index.htm" end
        Sendfile(client, path)
    end)
end)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用新的 SDK (NodeMCU) 发送多个数据 (conn:send()) 的相关文章

随机推荐