refactor client options
This commit is contained in:
parent
8c405cee88
commit
a87214cf06
@ -112,7 +112,7 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate {
|
|||||||
|
|
||||||
public convenience init(client: SocketEngineClient, url: String, options: NSDictionary?) {
|
public convenience init(client: SocketEngineClient, url: String, options: NSDictionary?) {
|
||||||
self.init(client: client, url: url,
|
self.init(client: client, url: url,
|
||||||
options: SocketIOClientOption.NSDictionaryToSocketOptionsSet(options ?? [:]))
|
options: Set<SocketIOClientOption>.NSDictionaryToSocketOptionsSet(options ?? [:]))
|
||||||
}
|
}
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
@ -210,7 +210,6 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate {
|
|||||||
let wsUrl = urlWebSocket + (sid == "" ? "" : "&sid=\(sid)")
|
let wsUrl = urlWebSocket + (sid == "" ? "" : "&sid=\(sid)")
|
||||||
|
|
||||||
ws = WebSocket(url: NSURL(string: wsUrl)!)
|
ws = WebSocket(url: NSURL(string: wsUrl)!)
|
||||||
|
|
||||||
if cookies != nil {
|
if cookies != nil {
|
||||||
let headers = NSHTTPCookie.requestHeaderFieldsWithCookies(cookies!)
|
let headers = NSHTTPCookie.requestHeaderFieldsWithCookies(cookies!)
|
||||||
for (key, value) in headers {
|
for (key, value) in headers {
|
||||||
|
|||||||
@ -105,7 +105,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
|
|||||||
*/
|
*/
|
||||||
public convenience init(socketURL: String, options: NSDictionary?) {
|
public convenience init(socketURL: String, options: NSDictionary?) {
|
||||||
self.init(socketURL: socketURL,
|
self.init(socketURL: socketURL,
|
||||||
options: SocketIOClientOption.NSDictionaryToSocketOptionsSet(options ?? [:]))
|
options: Set<SocketIOClientOption>.NSDictionaryToSocketOptionsSet(options ?? [:]))
|
||||||
}
|
}
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
|
|||||||
@ -24,9 +24,11 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
protocol ClientOptions {}
|
protocol ClientOption: CustomStringConvertible, Hashable {
|
||||||
|
func getSocketIOOptionValue() -> AnyObject?
|
||||||
|
}
|
||||||
|
|
||||||
public enum SocketIOClientOption: CustomStringConvertible, Hashable, ClientOptions {
|
public enum SocketIOClientOption: ClientOption {
|
||||||
case ConnectParams([String: AnyObject])
|
case ConnectParams([String: AnyObject])
|
||||||
case Cookies([NSHTTPCookie])
|
case Cookies([NSHTTPCookie])
|
||||||
case ExtraHeaders([String: String])
|
case ExtraHeaders([String: String])
|
||||||
@ -98,35 +100,13 @@ public enum SocketIOClientOption: CustomStringConvertible, Hashable, ClientOptio
|
|||||||
func getSocketIOOptionValue() -> AnyObject? {
|
func getSocketIOOptionValue() -> AnyObject? {
|
||||||
return Mirror(reflecting: self).children.first?.value as? AnyObject
|
return Mirror(reflecting: self).children.first?.value as? AnyObject
|
||||||
}
|
}
|
||||||
|
|
||||||
static func NSDictionaryToSocketOptionsSet(dict: NSDictionary) -> Set<SocketIOClientOption> {
|
|
||||||
var options = Set<SocketIOClientOption>()
|
|
||||||
|
|
||||||
for (rawKey, value) in dict {
|
|
||||||
if let key = rawKey as? String, opt = keyValueToSocketIOClientOption(key, value: value) {
|
|
||||||
options.insert(opt)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return options
|
|
||||||
}
|
|
||||||
|
|
||||||
static func SocketOptionsSetToNSDictionary(set: Set<SocketIOClientOption>) -> NSDictionary {
|
|
||||||
let options = NSMutableDictionary()
|
|
||||||
|
|
||||||
for option in set {
|
|
||||||
options[option.description] = option.getSocketIOOptionValue()
|
|
||||||
}
|
|
||||||
|
|
||||||
return options
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func ==(lhs: SocketIOClientOption, rhs: SocketIOClientOption) -> Bool {
|
public func ==(lhs: SocketIOClientOption, rhs: SocketIOClientOption) -> Bool {
|
||||||
return lhs.description == rhs.description
|
return lhs.description == rhs.description
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Set where Element: ClientOptions {
|
extension Set where Element: ClientOption {
|
||||||
mutating func insertIgnore(element: Element) {
|
mutating func insertIgnore(element: Element) {
|
||||||
let (insertType, _) = Mirror(reflecting: element).children.first!
|
let (insertType, _) = Mirror(reflecting: element).children.first!
|
||||||
for item in self {
|
for item in self {
|
||||||
@ -138,4 +118,26 @@ extension Set where Element: ClientOptions {
|
|||||||
|
|
||||||
self.insert(element)
|
self.insert(element)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static func NSDictionaryToSocketOptionsSet(dict: NSDictionary) -> Set<SocketIOClientOption> {
|
||||||
|
var options = Set<SocketIOClientOption>()
|
||||||
|
|
||||||
|
for (rawKey, value) in dict {
|
||||||
|
if let key = rawKey as? String, opt = SocketIOClientOption.keyValueToSocketIOClientOption(key, value: value) {
|
||||||
|
options.insert(opt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return options
|
||||||
|
}
|
||||||
|
|
||||||
|
func SocketOptionsSetToNSDictionary() -> NSDictionary {
|
||||||
|
let options = NSMutableDictionary()
|
||||||
|
|
||||||
|
for option in self {
|
||||||
|
options[option.description] = option.getSocketIOOptionValue()
|
||||||
|
}
|
||||||
|
|
||||||
|
return options
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user