Make server start and stop (start on a default or given port)

This commit is contained in:
Michael Bykovski 2017-07-08 12:15:41 +02:00
parent 203bd54167
commit 1459752b0d
6 changed files with 108 additions and 14 deletions

View File

@ -3,7 +3,7 @@
id="cordova-plugin-webserver" id="cordova-plugin-webserver"
version="1.0.0"> version="1.0.0">
<engines> <engines>
<engine name="cordova" version=">=3.0.0" /> <engine name="cordova" version=">=7.0.0" />
</engines> </engines>
<name>Webserver for Cordova Apps</name> <name>Webserver for Cordova Apps</name>
@ -35,7 +35,7 @@
<header-file src="src/ios/GCDWebServer/Core/GCDWebServer.h" /> <header-file src="src/ios/GCDWebServer/Core/GCDWebServer.h" />
<source-file src="src/ios/GCDWebServer/Core/GCDWebServer.m" /> <source-file src="src/ios/GCDWebServer/Core/GCDWebServer.m" />
<header-file src="src/ios/Webserver-Bridging-Header.h"/> <header-file src="src/ios/GCDWebServer-Bridging-Header.h"/>
<header-file src="src/ios/GCDWebServer/Core/GCDWebServerConnection.h" /> <header-file src="src/ios/GCDWebServer/Core/GCDWebServerConnection.h" />
<source-file src="src/ios/GCDWebServer/Core/GCDWebServerConnection.m" /> <source-file src="src/ios/GCDWebServer/Core/GCDWebServerConnection.m" />

View File

@ -0,0 +1,3 @@
#import "GCDWebServer.h"
#import "GCDWebServerDataRequest.h"
#import "GCDWebServerDataResponse.h"

View File

@ -1 +0,0 @@
#import "GCDWebServer.h"

View File

@ -1,19 +1,47 @@
@objc(Webserver) class Webserver : CDVPlugin { @objc(Webserver) class Webserver : CDVPlugin {
var webServer: GCDWebServer = GCDWebServer()
var request_ids: [String] = [] var request_ids: [String] = []
var webServer = GCDWebServer()
override func pluginInitialize() { override func pluginInitialize() {
self.request_ids = [] 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) { func start(_ command: CDVInvokedUrlCommand) {
self.request_ids.append("Hi") var port = 8080
let portArgument = command.argument(at: 0)
for request_id in self.request_ids { if portArgument != nil {
print(request_id) 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) let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK)
self.commandDelegate!.send(pluginResult, callbackId: command.callbackId) self.commandDelegate!.send(pluginResult, callbackId: command.callbackId)
} }

View File

@ -19,8 +19,21 @@ exports.defineAutoTests = function() {
}; };
exports.defineManualTests = function(contentEl, createActionButton) { exports.defineManualTests = function(contentEl, createActionButton) {
createActionButton('Start Webserver', function() { createActionButton('Start bljad Webserver', function() {
console.log(webserver); 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( webserver.start(
function() { function() {
@ -29,7 +42,20 @@ exports.defineManualTests = function(contentEl, createActionButton) {
function() { function() {
console.log('Error!'); console.log('Error!');
}, },
1337
);
});
createActionButton('Stop Webserver', function() {
console.log("Stopping webserver...");
webserver.stop(
function() {
console.log('Success!');
},
function() {
console.log('Error!');
}
); );
}); });
}; };

View File

@ -1,3 +1,41 @@
exports.start = function(success_callback, error_callback) { var WEBSERVER_CLASS = "Webserver";
cordova.exec(success_callback, error_callback, "Webserver", "start", []);
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",
[]
);
}; };