我正在尝试使用安全websocket(wss)将数据发送到云。云接口仅允许安全的websocket(不允许使用HTTP,非安全的websocket等)
我尝试了最新的master,dev build from nodemcu-build.com,但是SSL失败了。甚至HTTP也无法正常工作。所以我尝试了“1.5.4.1-final”版本。在这里,我验证了SSL适用于HTTPS:
conn=net.createConnection(net.TCP, 1)
> conn:on("receive", function(sck, c) print(c) end )
> conn:on("connection", function(conn)
print("connected")
conn:send("HEAD / HTTP/1.1\r\n"..
"Host: google.com\r\n"..
"Accept: */*\r\n"..
>> "User-Agent: Mozilla/4.0 (compatible; esp8266 Lua;)"..
"\r\n\r\n")
end )
> conn:on("disconnection", function(conn) print("disconnected") end )
> conn:connect(443,"google.com")
> connected
HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Referrer-Policy: no-referrer
Location: https://www.google.co.in/?gfe_rd=cr&ei=HWhHWffUGojT8ge-wrS4Cw
Content-Length: 262
Date: Mon, 19 Jun 2017 05:58:53 GMT
Alt-Svc: quic=":443"; ma=2592000; v="38,37,36,35"
但是当我尝试非安全的websocket和安全的websocket时,只有非安全的websocket才能工作。这是成功的非安全连接的日志:
ws = websocket.createClient()
> ws:on("connection", function()
print('connection opened', status)
end)
> ws:on("receive", function(_, msg, opcode)
print('got message:', msg, opcode) -- opcode is 1 for text message, 2 for binary
end)
> ws:on("close", function(_, status)
print('connection closed', status)
ws = nil -- required to lua gc the websocket client
end)
> ws:connect('ws://echo.websocket.org/')
> connection opened nil
这是因为安全连接失败:
> ws = websocket.createClient()
> ws:on("connection", function()
>> print('connection opened', status)
end)
> ws:on("receive", function(_, msg, opcode)
print('got message:', msg, opcode) -- opcode is 1 for text message, 2 for binary
end)
> ws:on("close", function(_, status)
>> print('connection closed', status)
ws = nil -- required to lua gc the websocket client
end)
> ws:connect('wss://echo.websocket.org/')
> connection closed -128
nodemcu中是否不支持安全websocket?如果没有,我如何获得安全的websocket工作?与非安全相比,安全websocket是否需要额外的代码?