Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua NodeMCU HTTP服务器停止响应_Lua_Esp8266_Nodemcu - Fatal编程技术网

Lua NodeMCU HTTP服务器停止响应

Lua NodeMCU HTTP服务器停止响应,lua,esp8266,nodemcu,Lua,Esp8266,Nodemcu,我正在尝试用NodeMCU创建一个简单的HTTP服务器。我启动nodeMCU,然后将其连接到wifi,然后运行下面的程序。我可以从浏览器连接到服务器。如果我继续重新加载页面,它将永远工作,但当我停止发送请求一两分钟后,服务器将不知何故停止工作。这意味着,当我重新加载页面时,nodeMCU没有收到任何数据(并且无法返回任何数据) a=0 功能接收(连接,有效负载) a=a+1 打印(有效载荷) local content=“你好!自服务器启动以来”。。A.“已建立连接” 本地contentLeng

我正在尝试用NodeMCU创建一个简单的HTTP服务器。我启动nodeMCU,然后将其连接到wifi,然后运行下面的程序。我可以从浏览器连接到服务器。如果我继续重新加载页面,它将永远工作,但当我停止发送请求一两分钟后,服务器将不知何故停止工作。这意味着,当我重新加载页面时,nodeMCU没有收到任何数据(并且无法返回任何数据)

a=0
功能接收(连接,有效负载)
a=a+1
打印(有效载荷)
local content=“你好!自服务器启动以来”。。A.“已建立连接

” 本地contentLength=string.len(内容) 连接:发送(“HTTP/1.1 200确定\r\n内容长度:“…contentLength…”\r\n\r\n“。内容) 康涅狄格州:关闭 结束 功能连接(conn) 接通(“接收”,接收) 结束 srv=net.createServer(net.TCP,1) srv:侦听(8080,连接)
我做了一些事情:

  • 我已通过向nothing添加链接阻止浏览器请求favicon
  • 我将非活动客户端的超时设置为1,以防止浏览器长时间加载(浏览器一直加载到超时)
  • 我更新了代码以发送一些HTTP头
  • 我尝试在每次连接后关闭和打开服务器(这不好,因为如果您继续进行连接,即使没有此修复程序,它也不会停止工作)
  • 我添加了
    conn:close()

我正在运行预编译固件0.9.6-dev_20150704 integer。

首先,您不应该使用那些旧的0.9.x二进制文件。它们不再受支持,并且包含很多bug。从
dev
(1.5.1)或
master
(1.4)分支生成自定义固件:

在SDK版本>1.0的情况下(这是从当前分支构建时得到的)
conn:send
是完全异步的,即不能连续多次调用它。另外,不能在
conn:send()
之后立即调用
conn:close()
,因为在
send()
完成之前,套接字可能会关闭。相反,您可以侦听
sent
事件并在其回调中关闭套接字。如果你考虑这个问题,你的代码在最新的固件上运行良好。

异步发送的一种更优雅的方式记录在中。然而,该方法使用了更多堆,对于像您这样数据很少的简单情况,它不是必需的

下面是一个完整的例子,上面有
on(“sent”)
。注意,我将favicon更改为外部资源。如果您使用“/”,浏览器仍会对您的ESP8266发出额外请求

a = 0

function receive(conn, payload)
    print(payload) 
    a = a + 1

    local content="<!DOCTYPE html><html><head><link rel='icon' type='image/png' href='http://nodemcu.com/favicon.png' /></head><body><h1>Hello!</h1><p>Since the start of the server " .. a .. " connections were made</p></body></html>"
    local contentLength=string.len(content)

    conn:on("sent", function(sck) sck:close() end)
    conn:send("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length:" .. contentLength .. "\r\n\r\n" .. content)
end

function connection(conn) 
    conn:on("receive", receive)
end

srv=net.createServer(net.TCP, 1) 
srv:listen(8080, connection)
a=0
功能接收(连接,有效负载)
打印(有效载荷)
a=a+1
local content=“你好!自服务器启动以来”。。A.“已建立连接

” 本地contentLength=string.len(内容) conn:on(“已发送”,函数(sck)sck:close()结束) 连接:发送(“HTTP/1.1 200确定\r\n内容类型:text/html\r\n内容长度:“…contentLength..”\r\n\r\n“.content) 结束 功能连接(conn) 接通(“接收”,接收) 结束 srv=net.createServer(net.TCP,1) srv:侦听(8080,连接)
虽然我还没有尝试过,但您的回答完美地解决了我的问题,旧固件可能就是问题所在。好的,我现在正在运行一个新固件,我刚刚发现服务器停止响应的问题可能是由我的WiFi AP引起的。。。我的平板电脑有一些问题。现在,我尝试过几次从WiFi断开连接,然后在服务器停止响应并且服务器再次运行时再次连接。这实际上是我的第一个猜测…直到我看到您运行的是NodeMCU 0.9.6。我通过连接静态IP解决了平板电脑问题,但对于NodeMCU,它没有解决任何问题,所以我使用了NodeMCU工作正常的AP(当然现在无法访问internet)
wifi.sta.config
使用
auto=1
,对吗?
a = 0

function receive(conn, payload)
    print(payload) 
    a = a + 1

    local content="<!DOCTYPE html><html><head><link rel='icon' type='image/png' href='http://nodemcu.com/favicon.png' /></head><body><h1>Hello!</h1><p>Since the start of the server " .. a .. " connections were made</p></body></html>"
    local contentLength=string.len(content)

    conn:on("sent", function(sck) sck:close() end)
    conn:send("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length:" .. contentLength .. "\r\n\r\n" .. content)
end

function connection(conn) 
    conn:on("receive", receive)
end

srv=net.createServer(net.TCP, 1) 
srv:listen(8080, connection)