SpaceTown-Developers / Lemon-Gate

Expression Advanced a Lua sub language.
Other
15 stars 5 forks source link

Suggestion: httpRequest which returns http response as string #177

Closed szymski closed 10 years ago

szymski commented 10 years ago

Can you make a function like httpRequest, but which returns http response as string?

Like: string dupa = httpRequest("http://webpage.com")

adamnejm commented 10 years ago

httpRequest("http://webpage.com", function(string Body){ string dupa = Body }, function(){})

h3xcat commented 10 years ago

Not possiblet because there is a delay when you try get data from other server and you must have callback function. Same principle in lua http://wiki.garrysmod.com/page/http/Fetch.

szymski commented 10 years ago

just freeze lemon until it'll have response

TheFreezebug commented 10 years ago

In going to be the first one to say ABSOLUTELY NOT. (That way nobody else has to)

h3xcat commented 10 years ago

You can't "just freeze lemongate" as it runs on pure gmod lua(which is not freezable). Although you can achieve it using lua coroutines but it would ruin performence as you would have to rewrite whole lemongate compiler and write multiple hacks to achieve it, at the end you will have lemongate that does freeze on httpRequest but run lemongate itself 100 times slower, just because you want httpRequest to wait for response rather using callbacks.

Also some people(including me) don't want the lemongate to freeze as we could execute other stuff while the gate is waiting for http response, such as initialize EGP.

h3xcat commented 10 years ago

There, if you really want httpRequest to wait, use/edit following code

function httpRequestC(coroutine CR,string S){ // Only works if called inside coroutine
    string Return = ""
    httpRequest(S,function(string Body){        
        Return = Body
        CR:resume()
    },function(){
        Return = ""
        CR:resume()
    })
    yield()
    return Return
}
@return: httpRequestC string

coroutine C = coroutine(function(){})
C = coroutine(function(){
    // Your code here <<<<===================
    print(httpRequestC(C,"http://example.com"))
    // And here
    sleep(2)
    print("hello world")
})
C:resume()
Rusketh commented 10 years ago

This is not going to happen. Closed.