Make server start and stop (start on a default or given port)
This commit is contained in:
parent
203bd54167
commit
1459752b0d
@ -3,7 +3,7 @@
|
||||
id="cordova-plugin-webserver"
|
||||
version="1.0.0">
|
||||
<engines>
|
||||
<engine name="cordova" version=">=3.0.0" />
|
||||
<engine name="cordova" version=">=7.0.0" />
|
||||
</engines>
|
||||
|
||||
<name>Webserver for Cordova Apps</name>
|
||||
@ -35,7 +35,7 @@
|
||||
<header-file src="src/ios/GCDWebServer/Core/GCDWebServer.h" />
|
||||
<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" />
|
||||
<source-file src="src/ios/GCDWebServer/Core/GCDWebServerConnection.m" />
|
||||
|
3
src/ios/GCDWebServer-Bridging-Header.h
Normal file
3
src/ios/GCDWebServer-Bridging-Header.h
Normal file
@ -0,0 +1,3 @@
|
||||
#import "GCDWebServer.h"
|
||||
#import "GCDWebServerDataRequest.h"
|
||||
#import "GCDWebServerDataResponse.h"
|
@ -1 +0,0 @@
|
||||
#import "GCDWebServer.h"
|
@ -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")
|
||||
var port = 8080
|
||||
let portArgument = command.argument(at: 0)
|
||||
|
||||
for request_id in self.request_ids {
|
||||
print(request_id)
|
||||
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)
|
||||
}
|
||||
|
@ -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!');
|
||||
}
|
||||
);
|
||||
});
|
||||
};
|
||||
|
42
webserver.js
42
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",
|
||||
[]
|
||||
);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user