This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
DannySMc's profile picture

Asynchronous HTTP?

Started by DannySMc, 19 March 2015 - 09:16 AM
DannySMc #1
Posted 19 March 2015 - 10:16 AM
I need another question, so I am trying to make a HTTP based chat, but… This is the tricky part, how would I make it feel seamless?

I thought maybe using http.request() and then it would do it's think automatically but it would still slow the program down, right? I was told by oeed that it is possible?

My question is how? Please examples or help would be great thanks.
KingofGamesYami #2
Posted 19 March 2015 - 01:53 PM
Sends a HTTP request to a website, asynchronously. Returns immediately, with an http_success or http_failure event being delivered later to indicate the outcome. Issues a POST request if postData is provided, or a GET request if it is nil (or omitted). headers must come as a third argument, if you need to make a GET request using custom headers just pass nil as the second argument.

http.request() would work, just work it in with an event loop and your good. It returns immediately, so it shouldn't slow the program down.

Example:


while true do
  local event = {os.pullEvent()}
  if event[ 1 ] == "char" then
    --#stuff
  elseif event[ 1 ] == "key" then
    --#stuff
  elseif event[ 1 ] == "http_success" then
    --#update chat
    http.request() --#I don't know what to give it
  elseif event[ 1 ] == "http_failure" then
    http.request() --#start a new request
  end
end
DannySMc #3
Posted 20 March 2015 - 09:12 AM
Sends a HTTP request to a website, asynchronously. Returns immediately, with an http_success or http_failure event being delivered later to indicate the outcome. Issues a POST request if postData is provided, or a GET request if it is nil (or omitted). headers must come as a third argument, if you need to make a GET request using custom headers just pass nil as the second argument.

http.request() would work, just work it in with an event loop and your good. It returns immediately, so it shouldn't slow the program down.

Example:


while true do
  local event = {os.pullEvent()}
  if event[ 1 ] == "char" then
	--#stuff
  elseif event[ 1 ] == "key" then
	--#stuff
  elseif event[ 1 ] == "http_success" then
	--#update chat
	http.request() --#I don't know what to give it
  elseif event[ 1 ] == "http_failure" then
	http.request() --#start a new request
  end
end

Ahh okay, I get that! So a http.request() call will run while I can still use any other input?
KingofGamesYami #4
Posted 20 March 2015 - 01:12 PM
Yes, until an http_success or http_failure event is thrown. http.get and http.post use http.request internally, so if you don't want to use http.request directly, you could utilize the parallel api or a coroutine manager.