Jump to content




Asynchronous HTTP?

lua networking utility

3 replies to this topic

#1 Dahknee

  • Members
  • 1,808 posts
  • Location/home/da

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.

#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 19 March 2015 - 01:53 PM

Quote

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


#3 Dahknee

  • Members
  • 1,808 posts
  • Location/home/da

Posted 20 March 2015 - 09:12 AM

 KingofGamesYami, on 19 March 2015 - 01:53 PM, said:

Quote

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?

#4 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users