我需要我的SWIFT2.1应用程序来监听由另一个服务到达我的公共端点的HTTP帖子。不知道如何初始化GCDWebserver。
let webServer = GCDWebServer()我试过:
webServer.addHandlerForMethod("POST", path: "https://myendpoint.com",
requestClass: GCDWebServerRequest.self, asyncProcessBlock: { request in
print("WebServer - POST detected")
})
webServer.startWithPort(80, bonjourName: "Web Server")和
try! webServer.startWithOptions([GCDWebServerOption_BonjourName: "", GCDWebServerOption_BonjourType: "https://myendpoint.com", GCDWebServerOption_Port : 80, GCDWebServerOption_AutomaticallySuspendInBackground: false])和
webServer.addHandlerForMethod("POST", path:"https://myendpoint.com", requestClass: GCDWebServerURLEncodedFormRequest.self, asyncProcessBlock: {request in
print("WebServer: POST captured")
})
webServer.start()但我哪儿也不会去。
无论我尝试什么,URL属性都指向我的localhost,而publicURL总是为零。
有小费吗?
发布于 2016-10-28 09:57:31
"path"将是http://<yourdeviceip>:<port>/path发布于 2016-10-28 10:01:43
您的配置是不正确的,其背后的想法是您的应用程序变成了一个服务器本身,您可以配置它来接受传入的请求。
在您的addHandlerForMethod中,您将路径设置为"https://myendpoint.com/":这是没有意义的。
您的应用程序将创建一个新服务器,因此它将生成自己的服务器URL (这应该是设备的ip,然后是您选择的自定义端口)。
我在Swift 2.3中使用它,所以语法本身可能有点不同,但是这个想法是:
// Create the server
let webServer = GCDWebServer()
// Configure different paths
let path = "/action"
webServer.addHandlerForMethod("POST", path: action, requestClass: GCDWebServerRequest.self, asyncProcessBlock: { request in
print("WebServer - POST detected")
return GCDWebServerResponse(statusCode: 200)
})
// Start server on port 8080
webServer.startWithPort(8080, bonjourName: nil)
// Print server url
print("Server url: \(webServer.serverURL)")使用此配置,您的服务器将能够在路径上接收POST请求。
http://<your-device-ip>:8080/actionhttps://stackoverflow.com/questions/40298765
复制相似问题