Starting server and stopping works.

Receiving receiving needs improvements
This commit is contained in:
Michael Bykovski 2017-07-10 09:58:47 +02:00
parent 1459752b0d
commit 5c160eee16
4 changed files with 88 additions and 18 deletions

View File

@ -1,3 +1,5 @@
#import "GCDWebServer.h"
#import "GCDWebServerDataRequest.h"
#import "GCDWebServerDataResponse.h"
#import "GCDWebServerResponse.h"
#import "GCDWebServerRequest.h"

View File

@ -1,34 +1,75 @@
@objc(Webserver) class Webserver : CDVPlugin {
// Timeout in seconds
let TIMEOUT: Int = 60 * 3 * 1000000
var webServer: GCDWebServer = GCDWebServer()
var request_ids: [String] = []
var responses: Dictionary<String, Any> = [:]
var onRequestCommand: CDVInvokedUrlCommand? = nil
override func pluginInitialize() {
self.request_ids = []
self.webServer = GCDWebServer()
self.initHTTPRequestHandlers()
self.onRequestCommand = nil
}
func requestToRequestDict(requestUUID: String, request: GCDWebServerDataRequest) -> Dictionary<String, Any> {
return [
"requestId": requestUUID,
"body": request.jsonObject ?? "",
"headers": request.headers,
"method": request.method,
"path": request.url.path,
"query": request.url.query ?? ""
]
}
func processRequest(request: GCDWebServerRequest) -> GCDWebServerResponse {
var timeout = 0
// Fetch data as GCDWebserverDataRequest
let dataRequest = request as! GCDWebServerDataRequest
let requestUUID = UUID().uuidString
// Transform it into an dictionary for the javascript plugin
let requestDict = self.requestToRequestDict(requestUUID: requestUUID, request: dataRequest)
// Do a call to the onRequestCommand to inform the JS plugin
if (self.onRequestCommand != nil) {
self.commandDelegate.send(
CDVPluginResult(status: CDVCommandStatus_OK, messageAs: requestDict),
callbackId: self.onRequestCommand?.callbackId
)
}
// Here we have to wait until the javascript block fetches the message and do a response
while self.responses[requestUUID] == nil && timeout < self.TIMEOUT {
timeout += 2000
usleep(2000)
}
let response = GCDWebServerResponse()
return response
}
func onRequest(_ command: CDVInvokedUrlCommand) {
self.onRequestCommand = command
}
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!
}
processBlock: self.processRequest
)
}
}
func sendResponse(_ command: CDVInvokedUrlCommand) {
self.responses[command.argument(at: 0) as! String] = command.argument(at: 1)
}
func start(_ command: CDVInvokedUrlCommand) {
var port = 8080
let portArgument = command.argument(at: 0)
if portArgument != nil {
port = portArgument as! Int
}
@ -36,7 +77,7 @@
let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK)
self.commandDelegate!.send(pluginResult, callbackId: command.callbackId)
}
func stop(_ command: CDVInvokedUrlCommand) {
if self.webServer.isRunning {
self.webServer.stop()

View File

@ -16,12 +16,34 @@ exports.defineAutoTests = function() {
});
})
});
describe('Do a request', function() {
it('should do a request', function() {
webserver.onRequest(
function(request) {
// Check for a request is made
}
);
websever.start();
webserver.stop();
});
});
};
exports.defineManualTests = function(contentEl, createActionButton) {
createActionButton('Start bljad Webserver', function() {
console.log("Starting webserver...");
console.log(webserver);
webserver.onRequest(
function(request) {
console.log(request);
webserver.sendResponse(request.requestId, {});
}
);
webserver.start(
function() {
console.log('Success!');

View File

@ -10,23 +10,28 @@ exports.start = function(success_callback, error_callback, port) {
);
};
exports.onRequest = function(success_callback, error_callback) {
exports.onRequest = function(success_callback) {
cordova.exec(
success_callback,
error_callback,
function(error) {console.error(error);}
WEBSERVER_CLASS,
"onRequest",
[]
);
};
exports.send = function(params, success_callback, error_callback) {
exports.sendResponse = function(
responseId,
params,
success_callback,
error_callback
) {
cordova.exec(
success_callback,
error_callback,
WEBSERVER_CLASS,
"send",
[params]
[requestId, params]
);
};