Skip to content

Conversation

@du-song
Copy link

@du-song du-song commented Mar 2, 2012

call response.enableAsync() will allow response to be written a while after the server callback is returned.

var httpServer = Titanium.Network.createHTTPServer();
httpServer.bind(8080, function(request, response)
{
    Titanium.API.debug("Got request for URI: " + request.getURI());
    response.setContentType("text/html");
    if (request.getURI().match(/async/)) {
        response.enableAsync(); // NOTE response.write() can only be invoked once.
        setTimeout(function(){
            response.write("<h1>Hello from Async</h1>");
        }, 1000);
    } else {
        response.write("<h1>Hello!</h1>");
    }
});

@Silviu-Marian
Copy link

Is there any way to recompile just the Network module?
Also, to prevent crash on second write invocation do something like this in the handler:

(function(){ 
    response._writefn = response.write; 
    response.oneoff=false; 
    response.write = function(){
        if(response.oneoff) return false;
        response.oneoff=true;
        return response._writefn.apply(this,arguments);
    };
}());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants