Skip to content
This repository was archived by the owner on Apr 21, 2020. It is now read-only.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ jspm_packages

# Optional REPL history
.node_repl_history

.idea
12 changes: 6 additions & 6 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
compileSdkVersion 26
buildToolsVersion "27.0.3"

defaultConfig {
minSdkVersion 16
targetSdkVersion 22
targetSdkVersion 26
versionCode 2
versionName "1.1"
ndk {
Expand All @@ -19,7 +19,7 @@ android {
}

dependencies {
compile 'com.facebook.react:react-native:+'
compile 'com.google.android.gms:play-services-gcm:+'
compile 'org.nanohttpd:nanohttpd:2.2.0'
implementation 'com.facebook.react:react-native:+'
implementation 'com.google.android.gms:play-services-gcm:16.0.0'
implementation 'org.nanohttpd:nanohttpd:2.2.0'
}
175 changes: 66 additions & 109 deletions android/src/main/java/com/strainy/RNHttpServer/RNHttpServerModule.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.strainy.RNHttpServer;

import com.strainy.RNHttpServer.Server;

import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReadableMap;
Expand All @@ -18,138 +17,96 @@
import java.util.Map;
import java.util.HashMap;

import android.support.annotation.Nullable;
import android.util.Log;

public class RNHttpServerModule extends ReactContextBaseJavaModule implements LifecycleEventListener {

ReactApplicationContext reactContext;

private static final String TAG = "RNHttpServer";

private static final String DEFAULT_PORT_KEY = "DEFAULT_PORT";
private static final String DEFAULT_TIMEOUT_KEY = "DEFAULT_TIMEOUT";
private static final String SERVER_EVENT_ID_KEY = "SERVER_EVENT";

private static final int DEFAULT_PORT = 8080;
private static final int DEFAULT_TIMEOUT = 30000;
private static final String SERVER_EVENT_ID = "reactNativeHttpServerResponse";

private int _port;
private int _timeout;
public class RNHttpServerModule extends ReactContextBaseJavaModule {

private Server _server = null;
ReactApplicationContext reactContext;

private Callback _success;
private Callback _failure;
private static final String TAG = "RNHttpServer";

public RNHttpServerModule(ReactApplicationContext reactContext) {
super(reactContext);
private static final String DEFAULT_PORT_KEY = "DEFAULT_PORT";
private static final String DEFAULT_TIMEOUT_KEY = "DEFAULT_TIMEOUT";
private static final String SERVER_EVENT_ID_KEY = "SERVER_EVENT";

_port = DEFAULT_PORT;
_timeout = DEFAULT_TIMEOUT;
private static final int DEFAULT_PORT = 8080;
private static final int DEFAULT_TIMEOUT = 30000;
private static final String SERVER_EVENT_ID = "reactNativeHttpServerResponse";

reactContext.addLifecycleEventListener(this);
private static final String ERROR_SERVER_NOT_RUNNING = "ERROR_SERVER_NOT_RUNNING";
private static final String ERROR_COULD_NOT_START = "ERROR_COULD_NOT_START";

this.reactContext = reactContext;
private int port;
private int timeout;
private Server server = null;

}

@Override
public String getName() {
return "RNHttpServer";
}
public RNHttpServerModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;

@Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
constants.put(DEFAULT_PORT_KEY, DEFAULT_PORT);
constants.put(DEFAULT_TIMEOUT_KEY, DEFAULT_TIMEOUT);
constants.put(SERVER_EVENT_ID_KEY, SERVER_EVENT_ID);
return constants;
}

@ReactMethod
public void init(ReadableMap options, Callback success, @Nullable Callback failure) {

Log.d(TAG, "Initialising server...");

if(options.hasKey("port")) {
_port = options.getInt("port");
port = DEFAULT_PORT;
timeout = DEFAULT_TIMEOUT;
}

if(options.hasKey("timeout")) {
_timeout = options.getInt("timeout");
@Override
public String getName() {
return "RNHttpServer";
}

start(success, failure);

}

@ReactMethod
public void start(@Nullable Callback success, @Nullable Callback failure) {
@Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
constants.put(DEFAULT_PORT_KEY, DEFAULT_PORT);
constants.put(DEFAULT_TIMEOUT_KEY, DEFAULT_TIMEOUT);
constants.put(SERVER_EVENT_ID_KEY, SERVER_EVENT_ID);

try {
return constants;
}

_server = new Server(reactContext, _port, _timeout);
_server.start();
@ReactMethod
public void init(ReadableMap options) {
Log.d(TAG, "Initializing server...");

if(success != null) {
success.invoke();
if (options.hasKey("port")) {
port = options.getInt("port");
}

}
catch(Exception e) {
Log.e(TAG, e.getMessage());

if(failure != null) {
failure.invoke(e.getMessage());
if (options.hasKey("timeout")) {
timeout = options.getInt("timeout");
}

}

}

@ReactMethod
public void stop() {
Log.d(TAG, "Server Stopped.");
_server.stop();
_server = null;
}

@ReactMethod
public void setResponse(String uri, ReadableMap response) {
if(_server != null) {
_server.setResponse(uri, response);
}
}

@ReactMethod
public String getHostName() {
if(_server != null) {
Log.d(TAG, _server.getHostname());
return _server.getHostname();
}
else {
return "not defined";
@ReactMethod
public void start(Promise promise) {
try {
server = new Server(reactContext, port, timeout);
server.start();
port = server.getListeningPort();

WritableMap args = Arguments.createMap();
args.putString("hostname", server.getHostname());
args.putString("port", String.valueOf(server.getListeningPort()));

promise.resolve(args);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
promise.reject(ERROR_COULD_NOT_START, e);
}
}
}

/* Shut down the server if app is destroyed or paused */
@Override
public void onHostResume() {
//we can restart the server here as the success callback is not needed since an event is registered
start(null, null);
}

@Override
public void onHostPause() {
stop();
}
@ReactMethod
public void stop() {
if (server == null) { return; }

@Override
public void onHostDestroy() {
stop();
}
server.stop();
server = null;
Log.d(TAG, "Server stopped.");
}

@ReactMethod
public void setResponse(String uri, ReadableMap response) {
if (server == null) { return; }
server.setResponse(uri, response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ public List<NativeModule> createNativeModules(
return modules;
}

@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}

@Override
public List<ViewManager> createViewManagers(
ReactApplicationContext reactContext) {
Expand Down
Loading