Skip to content

Commit

Permalink
Call error callback if server can't start
Browse files Browse the repository at this point in the history
  • Loading branch information
boedy committed May 13, 2019
1 parent b57350f commit 4e60018
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/android/NanoHTTPDWebserver.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
Expand Down
20 changes: 15 additions & 5 deletions src/android/Webserver.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
}
return true;
}
else if ("stop".equals(action)) {
if ("stop".equals(action)) {
this.stop(args, callbackContext);
return true;
}
else if ("onRequest".equals(action)) {
if ("onRequest".equals(action)) {
this.onRequest(args, callbackContext);
return true;
}
else if ("sendResponse".equals(action)) {
if ("sendResponse".equals(action)) {
this.sendResponse(args, callbackContext);
return true;
}
Expand All @@ -57,11 +57,20 @@ private void start(JSONArray args, CallbackContext callbackContext) throws JSONE

if (args.length() == 1) {
port = args.getInt(0);
}

if (this.nanoHTTPDWebserver != null){
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "Server already running"));
return;
}

this.nanoHTTPDWebserver = new NanoHTTPDWebserver(port, this);
this.nanoHTTPDWebserver.start();
try {
this.nanoHTTPDWebserver = new NanoHTTPDWebserver(port, this);
this.nanoHTTPDWebserver.start();
}catch (Exception e){
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, e.getMessage()));
return;
}

Log.d(
this.getClass().getName(),
Expand All @@ -80,6 +89,7 @@ private void start(JSONArray args, CallbackContext callbackContext) throws JSONE
private void stop(JSONArray args, CallbackContext callbackContext) throws JSONException {
if (this.nanoHTTPDWebserver != null) {
this.nanoHTTPDWebserver.stop();
this.nanoHTTPDWebserver = null;
}
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
}
Expand Down
6 changes: 6 additions & 0 deletions src/ios/Webserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@
if portArgument != nil {
port = portArgument as! Int
}

if self.webServer.isRunning{
self.commandDelegate!.send(CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: "Server already running"), callbackId: command.callbackId)
return
}

do {
try self.webServer.start(options:[GCDWebServerOption_AutomaticallySuspendInBackground : false, GCDWebServerOption_Port: UInt(port)])
} catch let error {
Expand Down

0 comments on commit 4e60018

Please sign in to comment.