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?
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.
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.
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
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.
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...