From 1459752b0d1682f394812d456e768742e91ced83 Mon Sep 17 00:00:00 2001 From: Michael Bykovski Date: Sat, 8 Jul 2017 12:15:41 +0200 Subject: [PATCH] Make server start and stop (start on a default or given port) --- plugin.xml | 4 +-- src/ios/GCDWebServer-Bridging-Header.h | 3 ++ src/ios/Webserver-Bridging-Header.h | 1 - src/ios/Webserver.swift | 42 +++++++++++++++++++++----- tests/tests.js | 30 ++++++++++++++++-- webserver.js | 42 ++++++++++++++++++++++++-- 6 files changed, 108 insertions(+), 14 deletions(-) create mode 100644 src/ios/GCDWebServer-Bridging-Header.h delete mode 100644 src/ios/Webserver-Bridging-Header.h diff --git a/plugin.xml b/plugin.xml index a575ea6..8dcb990 100644 --- a/plugin.xml +++ b/plugin.xml @@ -3,7 +3,7 @@ id="cordova-plugin-webserver" version="1.0.0"> - + Webserver for Cordova Apps @@ -35,7 +35,7 @@ - + diff --git a/src/ios/GCDWebServer-Bridging-Header.h b/src/ios/GCDWebServer-Bridging-Header.h new file mode 100644 index 0000000..43e8d25 --- /dev/null +++ b/src/ios/GCDWebServer-Bridging-Header.h @@ -0,0 +1,3 @@ +#import "GCDWebServer.h" +#import "GCDWebServerDataRequest.h" +#import "GCDWebServerDataResponse.h" diff --git a/src/ios/Webserver-Bridging-Header.h b/src/ios/Webserver-Bridging-Header.h deleted file mode 100644 index b7deef4..0000000 --- a/src/ios/Webserver-Bridging-Header.h +++ /dev/null @@ -1 +0,0 @@ -#import "GCDWebServer.h" diff --git a/src/ios/Webserver.swift b/src/ios/Webserver.swift index 17f6220..f9e9850 100644 --- a/src/ios/Webserver.swift +++ b/src/ios/Webserver.swift @@ -1,19 +1,47 @@ @objc(Webserver) class Webserver : CDVPlugin { - + var webServer: GCDWebServer = GCDWebServer() var request_ids: [String] = [] - var webServer = GCDWebServer() override func pluginInitialize() { self.request_ids = [] + self.webServer = GCDWebServer() + self.initHTTPRequestHandlers() + } + + func initHTTPRequestHandlers() { + for methodType in ["GET", "POST", "PUT", "PATCH", "DELETE"] { + self.webServer.addDefaultHandler( + forMethod: methodType, + request: GCDWebServerDataRequest.self, + processBlock: { + (request) -> GCDWebServerResponse in + let json = ["hello": "world"] + print((request as! GCDWebServerDataRequest).jsonObject as Any) + let response = GCDWebServerDataResponse(jsonObject: json) + return response! + + } + ) + } } func start(_ command: CDVInvokedUrlCommand) { - self.request_ids.append("Hi") - - for request_id in self.request_ids { - print(request_id) + var port = 8080 + let portArgument = command.argument(at: 0) + + if portArgument != nil { + port = portArgument as! Int } - + self.webServer.start(withPort: UInt(port), bonjourName: nil) + let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK) + self.commandDelegate!.send(pluginResult, callbackId: command.callbackId) + } + + func stop(_ command: CDVInvokedUrlCommand) { + if self.webServer.isRunning { + self.webServer.stop() + } + print("Stopping webserver") let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK) self.commandDelegate!.send(pluginResult, callbackId: command.callbackId) } diff --git a/tests/tests.js b/tests/tests.js index 0685a0d..daf32f2 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -19,8 +19,21 @@ exports.defineAutoTests = function() { }; exports.defineManualTests = function(contentEl, createActionButton) { - createActionButton('Start Webserver', function() { - console.log(webserver); + createActionButton('Start bljad Webserver', function() { + console.log("Starting webserver..."); + + webserver.start( + function() { + console.log('Success!'); + }, + function() { + console.log('Error!'); + } + ); + }); + + createActionButton('Start Webserver with Port 1337', function() { + console.log("Starting webserver..."); webserver.start( function() { @@ -29,7 +42,20 @@ exports.defineManualTests = function(contentEl, createActionButton) { function() { console.log('Error!'); }, + 1337 + ); + }); + createActionButton('Stop Webserver', function() { + console.log("Stopping webserver..."); + + webserver.stop( + function() { + console.log('Success!'); + }, + function() { + console.log('Error!'); + } ); }); }; diff --git a/webserver.js b/webserver.js index 8a76b6e..af720a9 100644 --- a/webserver.js +++ b/webserver.js @@ -1,3 +1,41 @@ -exports.start = function(success_callback, error_callback) { - cordova.exec(success_callback, error_callback, "Webserver", "start", []); +var WEBSERVER_CLASS = "Webserver"; + +exports.start = function(success_callback, error_callback, port) { + cordova.exec( + success_callback, + error_callback, + WEBSERVER_CLASS, + "start", + [port] + ); +}; + +exports.onRequest = function(success_callback, error_callback) { + cordova.exec( + success_callback, + error_callback, + WEBSERVER_CLASS, + "onRequest", + [] + ); +}; + +exports.send = function(params, success_callback, error_callback) { + cordova.exec( + success_callback, + error_callback, + WEBSERVER_CLASS, + "send", + [params] + ); +}; + +exports.stop = function(success_callback, error_callback) { + cordova.exec( + success_callback, + error_callback, + WEBSERVER_CLASS, + "stop", + [] + ); };