Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ General ➜ Asynchronous http request

Asynchronous http request

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Candido   USA  (78 posts)  Bio
Date Sun 12 Sep 2010 08:54 PM (UTC)
Message
So I really appreciate lua socket being included with Mushclient now, and I've started incorporating some web functionality with http.request. Works just how you'd expect, but in a way that's my complaint. During that half second of time it takes to load the web page, Mushclient waits for it to finish and you experience "lag".

I'm not sure how many of you are familiar with the idea of AJAX, but that's a technique used on web pages to load stuff from other web pages without freezing the page's script while it waits. It runs asynchronously, so to speak. Basically, you have it start loading the page and delegate a function for it to call when it completes (with the info from the page as the function's argument). Meanwhile, the script continues running.

Anyone know of a way I might accomplish something similar in Mushclient?
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #1 on Sun 12 Sep 2010 09:15 PM (UTC)
Message
Not with the vanilla socket.http library, I believe. You'd need to start from the base socket library and set the socket mode to nonblocking, then pretty much reimplement socket.http on top of it (with the asynchronous bit in mind). It gets hairy because Lua isn't the host program here, MUSHclient is. And MUSHclient decides when a script runs, which could be never if there's no stimulus. Truly, the best you could do is start a request and poll for the result using a timer or OnPluginTick.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #2 on Sun 12 Sep 2010 10:59 PM (UTC)
Message
I know exactly what you are talking about (after all, MUSHclient and MUD servers use asynchronous comms) but this case is trickier. Basically I agree with everything Twisol said.

You might be able to do it with low-level comms (ie. set up a socket, fire off your request, and then do a "select" to wait for the response to arrive completely). You probably would need a coroutine, which yields when the response has not completely arrived. However to get back into the coroutine you would need some sort of timer (eg. the tick Twisol mentioned, or a timer that fires every 1/10 second or so). This could do another select to see if more data had arrived.

Then you have to handle the HTTP headers yourself. It could all be a bit of a pain.

This might be easier: start up "wget" in a os.execute situation (if that runs without blocking, not sure about that), and wait for the file to fully arrive, then read it in. Or something along those lines.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #3 on Sun 12 Sep 2010 11:18 PM (UTC)
Message
I don't believe os.execute is asynchronous, as it returns a process status code. I think os.popen is, but I note that the popen code is commented out in the MUSHclient source with a note that it's "broken". :D

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #4 on Mon 13 Sep 2010 03:09 AM (UTC)

Amended on Mon 13 Sep 2010 03:16 AM (UTC) by Nick Gammon

Message
Yes, os.popen is complete rubbish.

This looks asynchronous to me:


utils.shellexecute ("wget", 
                    "http://mushclient.com --output-document='" .. 
                    GetInfo (67) .. "temp.txt'", 
                    nil, "open", 0 )


As far as I could see, it returned immediately, but a second or so later the file temp.txt was written to in the Worlds directory, containing the contents of that web page.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #5 on Mon 13 Sep 2010 03:15 AM (UTC)
Message
Hmm, try it on a nonexistant web address, so it'll take a while before it times out. I think that'll be the easiest way to tell if it's asynchronous...

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #6 on Mon 13 Sep 2010 03:18 AM (UTC)
Message
If you put a Note afterwards, it appears immediately.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #7 on Mon 13 Sep 2010 03:22 AM (UTC)
Message
This also logs what happened so you can see if something goes wrong:


utils.shellexecute ("wget", 
                    "http://mushclient.com --output-document='" .. 
                     GetInfo (67) .. "wget_output.txt' -o '" ..
                     GetInfo (67) .. "wget.log'", nil, "open", 0 )


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


26,389 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.