0.4.0: HTTP responses, everything is ready for status-dev-cli feature/responses-support
This commit is contained in:
parent
16e24d1a82
commit
64b37fe7e5
|
@ -3,6 +3,8 @@ logs
|
|||
*.log
|
||||
npm-debug.log*
|
||||
.DS_Store
|
||||
*.iml
|
||||
/.idea
|
||||
|
||||
# Runtime data
|
||||
pids
|
||||
|
|
|
@ -53,6 +53,13 @@ public class HttpServerModule extends ReactContextBaseJavaModule implements Life
|
|||
}
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void respond(int code, String type, String body) {
|
||||
if (server != null) {
|
||||
server.respond(code, type, body);
|
||||
}
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void stop() {
|
||||
Log.d(MODULE_NAME, "Server stopped");
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package me.alwx.HttpServer;
|
||||
|
||||
import fi.iki.elonen.NanoHTTPD;
|
||||
import fi.iki.elonen.NanoHTTPD.Response;
|
||||
import fi.iki.elonen.NanoHTTPD.Response.Status;
|
||||
import com.facebook.react.bridge.ReactContext;
|
||||
import com.facebook.react.bridge.Arguments;
|
||||
import com.facebook.react.bridge.ReadableMap;
|
||||
|
@ -20,6 +22,7 @@ public class Server extends NanoHTTPD {
|
|||
|
||||
private Map<String, ReadableMap> response;
|
||||
private ReactContext reactContext;
|
||||
private Response requestResponse;
|
||||
|
||||
public Server(ReactContext context, int port) {
|
||||
super(port);
|
||||
|
@ -33,6 +36,8 @@ public class Server extends NanoHTTPD {
|
|||
public Response serve(IHTTPSession session) {
|
||||
Log.d(TAG, "Request received!");
|
||||
|
||||
requestResponse = null;
|
||||
|
||||
WritableMap request;
|
||||
try {
|
||||
request = fillRequestMap(session);
|
||||
|
@ -43,7 +48,19 @@ public class Server extends NanoHTTPD {
|
|||
}
|
||||
|
||||
this.sendEvent(reactContext, SERVER_EVENT_ID, request);
|
||||
return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, "OK");
|
||||
|
||||
while (requestResponse == null) {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (Exception e) {
|
||||
Log.d(TAG, "Exception while waiting: " + e);
|
||||
}
|
||||
}
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
public void respond(int code, String type, String body) {
|
||||
requestResponse = newFixedLengthResponse(Status.lookup(code), type, body);
|
||||
}
|
||||
|
||||
private WritableMap fillRequestMap(IHTTPSession session) throws Exception {
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="FacetManager">
|
||||
<facet type="android" name="Android">
|
||||
<configuration />
|
||||
</facet>
|
||||
</component>
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/gen" isTestSource="false" generated="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/java" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
|
@ -20,5 +20,9 @@ module.exports = {
|
|||
stop: function () {
|
||||
Server.stop();
|
||||
DeviceEventEmitter.removeListener('httpServerResponseReceived');
|
||||
},
|
||||
|
||||
respond: function (code, type, body) {
|
||||
Server.respond(code, type, body);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
|
||||
@interface RCTHttpServer : NSObject <RCTBridgeModule> {
|
||||
WGCDWebServer* _webServer;
|
||||
WGCDWebServerDataResponse* _requestResponse;
|
||||
}
|
||||
@end
|
||||
|
||||
|
@ -23,23 +24,28 @@ RCT_EXPORT_MODULE();
|
|||
RCT_EXPORT_METHOD(start:(NSInteger) port)
|
||||
{
|
||||
RCTLogInfo(@"Running HTTP bridge server: %d", port);
|
||||
|
||||
|
||||
dispatch_sync(dispatch_get_main_queue(), ^{
|
||||
_webServer = [[WGCDWebServer alloc] init];
|
||||
|
||||
|
||||
[_webServer addDefaultHandlerForMethod:@"POST"
|
||||
requestClass:[WGCDWebServerDataRequest class]
|
||||
processBlock:^WGCDWebServerResponse *(WGCDWebServerRequest* request) {
|
||||
|
||||
|
||||
WGCDWebServerDataRequest* dataRequest = (WGCDWebServerDataRequest*)request;
|
||||
|
||||
_requestResponse = NULL;
|
||||
|
||||
[self.bridge.eventDispatcher sendAppEventWithName:@"httpServerResponseReceived"
|
||||
body:@{@"postData": dataRequest.jsonObject,
|
||||
@"url": dataRequest.URL.relativeString}];
|
||||
|
||||
return [WGCDWebServerDataResponse responseWithStatusCode:200];
|
||||
|
||||
while (_requestResponse == NULL) {
|
||||
[NSThread sleepForTimeInterval:0.1f];
|
||||
}
|
||||
|
||||
return _requestResponse;
|
||||
}];
|
||||
|
||||
|
||||
[_webServer startWithPort:port bonjourName:nil];
|
||||
});
|
||||
}
|
||||
|
@ -47,7 +53,7 @@ RCT_EXPORT_METHOD(start:(NSInteger) port)
|
|||
RCT_EXPORT_METHOD(stop)
|
||||
{
|
||||
RCTLogInfo(@"Stopping HTTP bridge server");
|
||||
|
||||
|
||||
//dispatch_sync(dispatch_get_main_queue(), ^{
|
||||
if (_webServer != nil) {
|
||||
[_webServer stop];
|
||||
|
@ -57,4 +63,12 @@ RCT_EXPORT_METHOD(stop)
|
|||
//});
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(respond:(NSInteger) code
|
||||
type: (NSString *) type
|
||||
body: (NSString *) body)
|
||||
{
|
||||
NSData* data = [body dataUsingEncoding:NSUTF8StringEncoding];
|
||||
_requestResponse = [[WGCDWebServerDataResponse alloc] initWithData:data contentType:type];
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "react-native-http-bridge",
|
||||
"version": "0.3.0",
|
||||
"version": "0.4.0",
|
||||
"description": "A simple HTTP debug server for React Native apps",
|
||||
"main": "httpServer.js",
|
||||
"repository": {
|
||||
|
|
Loading…
Reference in New Issue