As the title says... Chat on...

User avatar
By ribs99
#8020 I hope someone can help as I'm pulling my hair out trying to get this to work. I'm using the LuaUploader and when the following code is executed it works just fine:

Code: Select allrequire('net')
conn=net.createConnection(net.TCP, 1)
conn:on("receive", function(conn, payload) print(payload) end )
conn:connect(443,"108.59.13.232")
conn:send("GET /1/messages.json HTTP/1.1\r\n")
conn:send("Host: api.pushover.net\r\n")
conn:send("Pragma: no-cache\r\n")
conn:send("Cache-Control: no-cache\r\n")
conn:send("Content-Type: application/x-www-form-urlencoded\r\n")
conn:send("Accept: */*\r\n\r\n")
conn:close()


But when i try to execute the above from a function or in a file it doesn't work. For example if I write the above to a file called test.lua and then run the following in the immediate window or from within init.lua nothing happens.

Code: Select alldofile("test.lua")


Does anyone have any idea what i'm doing wrong?

PS. I'm running: NodeMcu 0.9.5 build 20150105 powered by Lua 5.1.4

Thanks
User avatar
By carlhesp
#8108 LUA is asynchronous. Your close command is running before you send commands have a chance to do anything. I ran into the same issue not realising this. When you paste the code in via lualoader there is a delay between each line letting the code run correctly.

Basically you want your close command inside a listener for when the data has finished sending. There is a thread here somewhere with my code for an example.