Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

basic async support in Titanium.Network.createHTTPServer #6

Open
wants to merge 2 commits into from

2 participants

@freewizard

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>");
    }
});
@ketamynx

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
This page is out of date. Refresh to see the latest.
View
10 modules/ti.Network/HTTPServerRequestFactory.cpp
@@ -37,13 +37,15 @@ class HTTPRequestHandler : public Poco::Net::HTTPRequestHandler {
};
void HTTPRequestHandler::handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response) {
- // XXX(Josh): The request and response object's lifetime is limited to this functions call.
- // If the developer should keep a reference to these around past the callback lifetime and then
- // attempts to access it may result in a crash!
ValueList args;
args.push_back(Value::NewObject(new HTTPServerRequest(request)));
- args.push_back(Value::NewObject(new HTTPServerResponse(response)));
+ HTTPServerResponse * resp = new HTTPServerResponse(response);
+ args.push_back(Value::NewObject(resp));
+ resp->asyncState = 0;
RunOnMainThread(m_callback, args);
+ while (resp->asyncState > 0) {
+ Poco::Thread::sleep(5);
+ }
}
HTTPServerRequestFactory::HTTPServerRequestFactory(KMethodRef callback)
View
12 modules/ti.Network/HTTPServerResponse.cpp
@@ -35,6 +35,7 @@ HTTPServerResponse::HTTPServerResponse(Poco::Net::HTTPServerResponse &response)
SetMethod("addCookie",&HTTPServerResponse::AddCookie);
SetMethod("setHeader",&HTTPServerResponse::SetHeader);
SetMethod("write",&HTTPServerResponse::Write);
+ SetMethod("enableAsync",&HTTPServerResponse::EnableAsync);
}
void HTTPServerResponse::SetStatus(const ValueList& args, KValueRef result)
@@ -102,6 +103,7 @@ void HTTPServerResponse::SetHeader(const ValueList& args, KValueRef result)
void HTTPServerResponse::Write(const ValueList& args, KValueRef result)
{
std::ostream& ostr = response.send();
+ asyncState = -1;
if (args.at(0)->IsString())
{
@@ -123,4 +125,14 @@ void HTTPServerResponse::Write(const ValueList& args, KValueRef result)
}
}
+void HTTPServerResponse::EnableAsync(const ValueList& args, KValueRef result)
+{
+ if (asyncState >= 0) {
+ //int v = args.at(0)->ToInt();
+ asyncState = 1;
+ } else {
+ throw ValueException::FromString("response already written, no async allowed.");
+ }
+}
+
} // namespace Titanium
View
3  modules/ti.Network/HTTPServerResponse.h
@@ -25,7 +25,7 @@ namespace Titanium {
class HTTPServerResponse : public StaticBoundObject {
public:
HTTPServerResponse(Poco::Net::HTTPServerResponse &response);
-
+ int asyncState; //0:sync & waiting for response; 1:async & waiting for response; -1: got response
private:
void SetStatus(const ValueList& args, KValueRef result);
void SetReason(const ValueList& args, KValueRef result);
@@ -35,6 +35,7 @@ class HTTPServerResponse : public StaticBoundObject {
void AddCookie(const ValueList& args, KValueRef result);
void SetHeader(const ValueList& args, KValueRef result);
void Write(const ValueList& args, KValueRef result);
+ void EnableAsync(const ValueList& args, KValueRef result);
Poco::Net::HTTPServerResponse& response;
};
View
2  modules/ti.UI/mac/MenuDelegate.h
@@ -18,7 +18,7 @@
#include "MenuMac.h"
-@interface MenuDelegate : NSObject
+@interface MenuDelegate : NSObject<NSMenuDelegate>
{
Titanium::MenuMac* menu;
BOOL dirty;
View
2  modules/ti.UI/mac/NativeWindow.h
@@ -31,7 +31,7 @@ class UserWindowMac;
@class WebViewDelegate;
-@interface NativeWindow : NSWindow
+@interface NativeWindow : NSWindow<NSWindowDelegate>
{
BOOL canReceiveFocus;
WebView* webView;
View
2  modules/ti.UI/mac/TitaniumApplicationDelegate.h
@@ -18,7 +18,7 @@
#include "UIMac.h"
-@interface TitaniumApplicationDelegate : NSObject
+@interface TitaniumApplicationDelegate : NSObject<NSApplicationDelegate>
{
Titanium::UIMac *binding;
}
Something went wrong with that request. Please try again.