set timeout to 5s in SDK

This commit is contained in:
Zhigang Fang
2017-05-19 19:20:12 +08:00
parent 22819f06cf
commit 71a507ddb2

View File

@@ -36,6 +36,13 @@ enum Endpoint {
}
extension OSCKit {
static let urlSession: URLSession = {
let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = 5
return URLSession(configuration: config)
}()
func assembleRequest(endPoint: Endpoint = .execute, params json: JSON? = nil) -> URLRequest {
var request = URLRequest(url: URL(string: "http://192.168.1.1\(endPoint.path)")!)
request.httpMethod = endPoint.method
@@ -49,7 +56,7 @@ extension OSCKit {
func requestJSON(endPoint: Endpoint = .execute, params json: JSON? = nil) -> Promise<JSON> {
var request = assembleRequest(endPoint: endPoint, params: json)
request.addValue("application/json", forHTTPHeaderField: "Accept")
return URLSession.shared.dataTask(with: request).then(execute: { data -> JSON in
return OSCKit.urlSession.dataTask(with: request).then(execute: { data -> JSON in
let anyObject = try JSONSerialization.jsonObject(with: data, options: [])
return JSON(value: anyObject as? NSObject)
})
@@ -58,7 +65,7 @@ extension OSCKit {
func requestData(command: Command) -> Promise<Data> {
return async {
let request = self.assembleRequest(params: command.json)
return try await(URLSession.shared.dataTask(with: request))
return try await(OSCKit.urlSession.dataTask(with: request))
}
}