如何使用新的 SDK (NodeMCU) 发送多个数据 (conn:send()) [英] How to send multiple data (conn:send()) with the new SDK (NodeMCU)
问题描述
我一直在阅读 NodeMCU 文档和几个关于 SDK 更改的已解决问题,这些问题以前允许发送多个数据流(就像一个排队的 net.socket:send).
I've been reading the NodeMCU documentation and several closed issues about the change of SDK that previouly allowed to send multiple data streams (acting like a queued net.socket:send).
这里似乎引发了一场巨大的争论(#730) 和那里 (#993) 甚至这里 (#999).但是,我没有找到任何令人信服的网络服务器代码示例,可以让我读取多个 html 文件(例如 head.html
和 body.html
)以显示页面.这是我尝试改编的 TerryE 示例,但没有成功:
It seems a huge debate grew here (#730) and there (#993) or even here (#999). However, I did not find any convincing example of a webserver code that would allow me to read multiple html files (e.g. head.html
and body.html
) to display a page. Here's the example from TerryE that I tried to adapt, but with no success:
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 终端收到错误消息.
When connecting to the ESP8266, nothing loads and I get no error from the lua terminal.
供您参考,head.html
包含:
<html>
<head>
</head>
并且 body.html
包含:
<body>
<h1>Hello World</h1>
</body>
</html>
我对 NodeMCU 很陌生,请多多包涵.
I am very new to NodeMCU, please be tolerant.
推荐答案
感谢您的回复.我实际上添加了你提到的标题,我不知道这是必要的,我还删除了 sender
函数中的 sck
参数.我的第一个代码实际上可以运行,我不知道上次是什么问题.
Thank you for the reply. I actually added the header you mentioned, I didn't know that was necessary and I also removed the sck
argument in the sender
function. My first code was actually working, I don't know what was wrong last time.
无论如何,它帮助我理解发生了什么:以下代码似乎连接了 response
数组,因为套接字的事件 sent
回调了 sender
函数 (sck:on("sent", sender)
)
Anyway, it helped me understanding what was happening: the following code seems to concatenate the response
array, since the event sent
of the socket calls back the sender
function (sck:on("sent", sender)
)
sck:send(table.remove(response,1))
其实table.remove(array, 1)
返回的是数组的第一项,并移除了数组的该项.多次调用此行具有逐项通读的效果.
In fact, table.remove(array, 1)
returns the first item of the array, and removes this item of the array. Calling this line multiple times has the effect to read through it, item by item.
为了简单起见,下面是一个能够提供多个文件的简单网络服务器的代码:
For the sake of simplicity, here is the code of a simple webserver able to serve multiple files:
header = "HTTP/1.0 200 OK
Server: NodeMCU on ESP8266
Content-Type: text/html
"
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on ("receive", function(sck, req)
local response = {header}
tgtfile = string.sub(req,string.find(req,"GET /") +5,string.find(req,"HTTP/") -2 )
if tgtfile == "" then tgtfile = "index.htm" end
local f = file.open(tgtfile,"r")
if f ~= nil then
response[#response+1] = file.read()
file.close()
else
response[#response+1] = "<html>"
response[#response+1] = tgtfile.." not Found - 404 error."
response[#response+1] = "<a href='index.htm'>Home</a>"
end
collectgarbage()
f = nil
tgtfile = nil
local function sender ()
if #response>0 then sck:send(table.remove(response,1))
else sck:close()
end
end
sck:on("sent", sender)
sender()
end)
end)
这个例子取自这个 instructables 并固定到使用新的 SDK(不再允许多个 :send).如果此代码有问题,请告诉我.
This example was taken from this instructables and fixed to work with the new SDK (which do not allow multiple :send anymore). Please let me know if this code has some issues.
我不知道文件的大小限制是多少.尽管如此,我还是设法将超过 2Ko 附加到 response
变量并立即发送,没有任何问题.
I don't know what is the size limit of the files though. Nevertheless, I manage to append more than 2Ko to the response
variable and send it at once without any issue.
这篇关于如何使用新的 SDK (NodeMCU) 发送多个数据 (conn:send())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!