Starting server and stopping works.
Receiving receiving needs improvements
This commit is contained in:
parent
1459752b0d
commit
5c160eee16
@ -1,3 +1,5 @@
|
|||||||
#import "GCDWebServer.h"
|
#import "GCDWebServer.h"
|
||||||
#import "GCDWebServerDataRequest.h"
|
#import "GCDWebServerDataRequest.h"
|
||||||
#import "GCDWebServerDataResponse.h"
|
#import "GCDWebServerDataResponse.h"
|
||||||
|
#import "GCDWebServerResponse.h"
|
||||||
|
#import "GCDWebServerRequest.h"
|
||||||
|
@ -1,34 +1,75 @@
|
|||||||
@objc(Webserver) class Webserver : CDVPlugin {
|
@objc(Webserver) class Webserver : CDVPlugin {
|
||||||
|
// Timeout in seconds
|
||||||
|
let TIMEOUT: Int = 60 * 3 * 1000000
|
||||||
|
|
||||||
var webServer: GCDWebServer = GCDWebServer()
|
var webServer: GCDWebServer = GCDWebServer()
|
||||||
var request_ids: [String] = []
|
var responses: Dictionary<String, Any> = [:]
|
||||||
|
var onRequestCommand: CDVInvokedUrlCommand? = nil
|
||||||
|
|
||||||
override func pluginInitialize() {
|
override func pluginInitialize() {
|
||||||
self.request_ids = []
|
|
||||||
self.webServer = GCDWebServer()
|
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() {
|
func initHTTPRequestHandlers() {
|
||||||
for methodType in ["GET", "POST", "PUT", "PATCH", "DELETE"] {
|
for methodType in ["GET", "POST", "PUT", "PATCH", "DELETE"] {
|
||||||
self.webServer.addDefaultHandler(
|
self.webServer.addDefaultHandler(
|
||||||
forMethod: methodType,
|
forMethod: methodType,
|
||||||
request: GCDWebServerDataRequest.self,
|
request: GCDWebServerDataRequest.self,
|
||||||
processBlock: {
|
processBlock: self.processRequest
|
||||||
(request) -> GCDWebServerResponse in
|
|
||||||
let json = ["hello": "world"]
|
|
||||||
print((request as! GCDWebServerDataRequest).jsonObject as Any)
|
|
||||||
let response = GCDWebServerDataResponse(jsonObject: json)
|
|
||||||
return response!
|
|
||||||
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func sendResponse(_ command: CDVInvokedUrlCommand) {
|
||||||
|
self.responses[command.argument(at: 0) as! String] = command.argument(at: 1)
|
||||||
|
}
|
||||||
|
|
||||||
func start(_ command: CDVInvokedUrlCommand) {
|
func start(_ command: CDVInvokedUrlCommand) {
|
||||||
var port = 8080
|
var port = 8080
|
||||||
let portArgument = command.argument(at: 0)
|
let portArgument = command.argument(at: 0)
|
||||||
|
|
||||||
if portArgument != nil {
|
if portArgument != nil {
|
||||||
port = portArgument as! Int
|
port = portArgument as! Int
|
||||||
}
|
}
|
||||||
@ -36,7 +77,7 @@
|
|||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
func stop(_ command: CDVInvokedUrlCommand) {
|
func stop(_ command: CDVInvokedUrlCommand) {
|
||||||
if self.webServer.isRunning {
|
if self.webServer.isRunning {
|
||||||
self.webServer.stop()
|
self.webServer.stop()
|
||||||
|
@ -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) {
|
exports.defineManualTests = function(contentEl, createActionButton) {
|
||||||
createActionButton('Start bljad Webserver', function() {
|
createActionButton('Start bljad Webserver', function() {
|
||||||
console.log("Starting webserver...");
|
console.log("Starting webserver...");
|
||||||
|
|
||||||
|
console.log(webserver);
|
||||||
|
|
||||||
|
webserver.onRequest(
|
||||||
|
function(request) {
|
||||||
|
console.log(request);
|
||||||
|
webserver.sendResponse(request.requestId, {});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
webserver.start(
|
webserver.start(
|
||||||
function() {
|
function() {
|
||||||
console.log('Success!');
|
console.log('Success!');
|
||||||
|
13
webserver.js
13
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(
|
cordova.exec(
|
||||||
success_callback,
|
success_callback,
|
||||||
error_callback,
|
function(error) {console.error(error);}
|
||||||
WEBSERVER_CLASS,
|
WEBSERVER_CLASS,
|
||||||
"onRequest",
|
"onRequest",
|
||||||
[]
|
[]
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.send = function(params, success_callback, error_callback) {
|
exports.sendResponse = function(
|
||||||
|
responseId,
|
||||||
|
params,
|
||||||
|
success_callback,
|
||||||
|
error_callback
|
||||||
|
) {
|
||||||
cordova.exec(
|
cordova.exec(
|
||||||
success_callback,
|
success_callback,
|
||||||
error_callback,
|
error_callback,
|
||||||
WEBSERVER_CLASS,
|
WEBSERVER_CLASS,
|
||||||
"send",
|
"send",
|
||||||
[params]
|
[requestId, params]
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user