如何使用新SDK(NodeMCU)发送多个数据(conn:send()) [英] How to send multiple data (conn:send()) with the new SDK (NodeMCU)
问题描述
我一直在阅读NodeMCU文档以及几个有关SDK更改的封闭问题,这些问题以前允许发送多个数据流(表现为排队的net.socket:send)。
这里似乎引发了一场巨大的争论(#730
a>)和那里(#993 )甚至在这里(#999 )。然而,我没有找到一个令人信服的Web服务器代码示例,它允许我读取多个html文件(例如 head.html
和 body.html
)显示一个页面。这里是TerryE的例子,我试图去适应,但没有成功:$ $ $ $ $ $ $ $ srv = net.createServer(net.TCP)
srv:listen(80,函数(conn)
conn:on(receive,函数(sck,req)
本地响应= {}
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()
结束
本地函数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>
和 body.html
包含:
< body>
< h1> Hello World< / h1>
< / body>
< / html>
我对NodeMCU非常陌生,请耐心等待。
感谢您的回复。我实际上添加了你提到的头文件,我不知道这是必要的,我还删除了 无论如何,它帮助我理解发生了什么:下面的代码似乎将连接 事实上, 这个例子取自指导性的,并修复与新的SDK(不允许多个:发送)一起工作。请让我知道这段代码是否有问题。 我不知道文件的大小限制是多少。尽管如此,我还是设法在 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). 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. When connecting to the ESP8266, nothing loads and I get no error from the lua terminal. For your information, And I am very new to NodeMCU, please be tolerant. Thank you for the reply. I actually added the header you mentioned, I didn't know that was necessary and I also removed the Anyway, it helped me understanding what was happening: the following code seems to concatenate the In fact, For the sake of simplicity, here is the code of a simple webserver able to serve multiple files: 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. I don't know what is the size limit of the files though. Nevertheless, I manage to append more than 2Ko to the 这篇关于如何使用新SDK(NodeMCU)发送多个数据(conn:send())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!发件人$ c $中的
sck
参数功能。我的第一个代码实际上是工作的,我不知道上次出了什么问题。
响应
数组,因为套接字的事件发送
回调 sender $ c
$ b $ $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ c> sck:send(table.remove(response,1))
table.remove(array,1)
返回数组的第一个项目,并删除这个数组的项目。为了简单起见,下面是简单web服务器
的代码
/ strong>能够提供多个文件:
header =HTTP / 1.0 200 OK \r\\\
Server:NodeMCU on ESP8266 \r\\\
Content-Type:text / html\r\\\
\r\\\
srv = net.createServer(net.TCP)
srv:listen(80,函数(conn)
conn:on(receive,函数(sck,req)
本地响应= {标题}
tgtfile = string.sub(req,string。 find(req,GET /)+ 5,string.find(req,HTTP /)-2)
if tgtfile ==then tgtfile =index.htmend
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'>主页< / a>
结束
collectgarbage()
f = nil
tgtfile = nil
本地函数sender()
if#response> 0 then sck:send(table.remove(response,1))
else sck:close ()
end
end
sck:on(sent,sender)
sender()
end)
end)
响应
变量中添加超过2Ko,并立即发送。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 )
head.html
contains:<html>
<head>
</head>
body.html
contains:<body>
<h1>Hello World</h1>
</body>
</html>
sck
argument in the sender
function. My first code was actually working, I don't know what was wrong last time.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)
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.header = "HTTP/1.0 200 OK\r\nServer: NodeMCU on ESP8266\r\nContent-Type: text/html\r\n\r\n"
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)
response
variable and send it at once without any issue.