From 5c160eee16ada9b9509a0dccc4931fb8a6b47414 Mon Sep 17 00:00:00 2001 From: Michael Bykovski Date: Mon, 10 Jul 2017 09:58:47 +0200 Subject: [PATCH] Starting server and stopping works. Receiving receiving needs improvements --- src/ios/GCDWebServer-Bridging-Header.h | 2 + src/ios/Webserver.swift | 69 ++++++++++++++++++++------ tests/tests.js | 22 ++++++++ webserver.js | 13 +++-- 4 files changed, 88 insertions(+), 18 deletions(-) diff --git a/src/ios/GCDWebServer-Bridging-Header.h b/src/ios/GCDWebServer-Bridging-Header.h index 43e8d25..d6c72c0 100644 --- a/src/ios/GCDWebServer-Bridging-Header.h +++ b/src/ios/GCDWebServer-Bridging-Header.h @@ -1,3 +1,5 @@ #import "GCDWebServer.h" #import "GCDWebServerDataRequest.h" #import "GCDWebServerDataResponse.h" +#import "GCDWebServerResponse.h" +#import "GCDWebServerRequest.h" diff --git a/src/ios/Webserver.swift b/src/ios/Webserver.swift index f9e9850..36d1467 100644 --- a/src/ios/Webserver.swift +++ b/src/ios/Webserver.swift @@ -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 = [:] + 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 { + 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() diff --git a/tests/tests.js b/tests/tests.js index daf32f2..91a5799 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -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!'); diff --git a/webserver.js b/webserver.js index af720a9..ba7f18d 100644 --- a/webserver.js +++ b/webserver.js @@ -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] ); };