I am able to connect and send/receive data, but the socket close command doesn't seem to work correctly.
I noticed that I wasn't always seeing the print statement I had in the close callback and that the available heap kept getting smaller with every iteration. At some point I noticed that the close messages that I do get all have a "something went wrong" exit code -99, and I assume that is the root of my problem.
I simplified the code as much as possible so that now it's pretty much the same as the example code from the nodemcu.readthedocs page. I tried putting the ws:close call in the receive callback and I've also tried calling it from a tmr.create():alarm(1000, tmr.ALARM_SINGLE... function and the results are the same.
Here's the minimal version of the code that's failing:
local ws = websocket.createClient()
ws:on("connection", function(ws)
print('got ws connection')
ws:send('hello!')
end)
ws:on("receive", function(_, msg, opcode)
print('got message:', msg, opcode) -- opcode is 1 for text message, 2 for binary
ws:close()
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')
Here are the results from Lualoader:
dofile("websocket2.lua")
> got ws connection
got message: hello! 1
connection closed -99
I should have the latest version - I did a build from the master branch at nodemcu-build.com earlier this week.
Am I doing something wrong? Have other people successfully used websockets with this device?
Thanks
Glen