Call error callback if server can't start

This commit is contained in:
boedy 2019-05-13 15:08:15 +02:00
parent b57350f577
commit 4e600184e5
3 changed files with 23 additions and 6 deletions

View File

@ -13,6 +13,7 @@ import java.io.File;
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;

View File

@ -32,15 +32,15 @@ public class Webserver extends CordovaPlugin {
}
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;
}
@ -57,11 +57,20 @@ public class Webserver extends CordovaPlugin {
if (args.length() == 1) {
port = args.getInt(0);
}
this.nanoHTTPDWebserver = new NanoHTTPDWebserver(port, this);
this.nanoHTTPDWebserver.start();
if (this.nanoHTTPDWebserver != null){
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "Server already running"));
return;
}
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(),
@ -80,6 +89,7 @@ public class Webserver extends CordovaPlugin {
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));
}

View File

@ -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 {