From 4e600184e52d05ff0b6edd1f2843a71b21b2dce9 Mon Sep 17 00:00:00 2001
From: boedy <boudewijng@gmail.com>
Date: Mon, 13 May 2019 15:08:15 +0200
Subject: [PATCH] Call error callback if server can't start

---
 src/android/NanoHTTPDWebserver.java |  1 +
 src/android/Webserver.java          | 22 ++++++++++++++++------
 src/ios/Webserver.swift             |  6 ++++++
 3 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/src/android/NanoHTTPDWebserver.java b/src/android/NanoHTTPDWebserver.java
index 632a515..517f5d6 100644
--- a/src/android/NanoHTTPDWebserver.java
+++ b/src/android/NanoHTTPDWebserver.java
@@ -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;
diff --git a/src/android/Webserver.java b/src/android/Webserver.java
index cf82547..a80afb4 100644
--- a/src/android/Webserver.java
+++ b/src/android/Webserver.java
@@ -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));
     }
diff --git a/src/ios/Webserver.swift b/src/ios/Webserver.swift
index c6c4712..850a92c 100644
--- a/src/ios/Webserver.swift
+++ b/src/ios/Webserver.swift
@@ -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 {