From 47fd0e386f216b7cd274af01d80186c9df59248a Mon Sep 17 00:00:00 2001 From: Erik Date: Thu, 29 Oct 2015 18:25:45 -0400 Subject: [PATCH] further decouple engine and client --- .../xcschemes/SocketIO-tvOS.xcscheme | 8 ++-- SocketIO-MacTests/SocketEngineTest.swift | 2 +- SocketIOClientSwift/SocketEngine.swift | 26 +++++++------ SocketIOClientSwift/SocketEngineClient.swift | 3 -- SocketIOClientSwift/SocketEngineSpec.swift | 2 +- SocketIOClientSwift/SocketIOClient.swift | 4 +- .../SocketIOClientOption.swift | 39 ++++++++++++++----- 7 files changed, 53 insertions(+), 31 deletions(-) diff --git a/Socket.IO-Client-Swift.xcodeproj/xcshareddata/xcschemes/SocketIO-tvOS.xcscheme b/Socket.IO-Client-Swift.xcodeproj/xcshareddata/xcschemes/SocketIO-tvOS.xcscheme index b5bbe52..06aca28 100644 --- a/Socket.IO-Client-Swift.xcodeproj/xcshareddata/xcschemes/SocketIO-tvOS.xcscheme +++ b/Socket.IO-Client-Swift.xcodeproj/xcshareddata/xcschemes/SocketIO-tvOS.xcscheme @@ -15,7 +15,7 @@ @@ -57,7 +57,7 @@ @@ -79,7 +79,7 @@ @@ -97,7 +97,7 @@ diff --git a/SocketIO-MacTests/SocketEngineTest.swift b/SocketIO-MacTests/SocketEngineTest.swift index a29b1a0..2c065ea 100644 --- a/SocketIO-MacTests/SocketEngineTest.swift +++ b/SocketIO-MacTests/SocketEngineTest.swift @@ -15,7 +15,7 @@ class SocketEngineTest: XCTestCase { override func setUp() { super.setUp() client = SocketIOClient(socketURL: "") - engine = SocketEngine(client: client, options: nil) + engine = SocketEngine(client: client, url: "", options: nil) client.setTestable() } diff --git a/SocketIOClientSwift/SocketEngine.swift b/SocketIOClientSwift/SocketEngine.swift index fe74f27..5319d6a 100644 --- a/SocketIOClientSwift/SocketEngine.swift +++ b/SocketIOClientSwift/SocketEngine.swift @@ -27,7 +27,7 @@ import Foundation public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate { public private(set) var sid = "" public private(set) var cookies: [NSHTTPCookie]? - public private(set) var socketPath = "" + public private(set) var socketPath = "/engine.io" public private(set) var urlPolling = "" public private(set) var urlWebSocket = "" public private(set) var ws: WebSocket? @@ -42,6 +42,7 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate { private let handleQueue = dispatch_queue_create("com.socketio.engineHandleQueue", DISPATCH_QUEUE_SERIAL) private let logType = "SocketEngine" private let parseQueue = dispatch_queue_create("com.socketio.engineParseQueue", DISPATCH_QUEUE_SERIAL) + private let url: String private let workQueue = NSOperationQueue() private var closed = false @@ -62,6 +63,7 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate { private var postWait = [String]() private var probing = false private var probeWait = ProbeWaitQueue() + private var secure = false private var session: NSURLSession! private var voipEnabled = false private var waitingForPoll = false @@ -72,8 +74,9 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate { private(set) var polling = true private(set) var websocket = false - public init(client: SocketEngineClient, options: Set) { + public init(client: SocketEngineClient, url: String, options: Set) { self.client = client + self.url = url for option in options { switch option { @@ -93,6 +96,8 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate { extraHeaders = headers case .VoipEnabled(let enable): voipEnabled = enable + case .Secure(let secure): + self.secure = secure default: continue } @@ -105,8 +110,8 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate { } } - public convenience init(client: SocketEngineClient, options: NSDictionary?) { - self.init(client: client, + public convenience init(client: SocketEngineClient, url: String, options: NSDictionary?) { + self.init(client: client, url: url, options: SocketIOClientOption.NSDictionaryToSocketOptionsSet(options ?? [:])) } @@ -167,17 +172,16 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate { return ("", "") } - let path = socketPath == "" ? "/socket.io" : socketPath - let url = "\(client!.socketURL)\(path)/?transport=" + let socketURL = "\(url)\(socketPath)/?transport=" var urlPolling: String var urlWebSocket: String - if client!.secure { - urlPolling = "https://" + url + "polling" - urlWebSocket = "wss://" + url + "websocket" + if secure { + urlPolling = "https://" + socketURL + "polling" + urlWebSocket = "wss://" + socketURL + "websocket" } else { - urlPolling = "http://" + url + "polling" - urlWebSocket = "ws://" + url + "websocket" + urlPolling = "http://" + socketURL + "polling" + urlWebSocket = "ws://" + socketURL + "websocket" } if params != nil { diff --git a/SocketIOClientSwift/SocketEngineClient.swift b/SocketIOClientSwift/SocketEngineClient.swift index bc0f4ae..1f11549 100644 --- a/SocketIOClientSwift/SocketEngineClient.swift +++ b/SocketIOClientSwift/SocketEngineClient.swift @@ -26,9 +26,6 @@ import Foundation @objc public protocol SocketEngineClient { - var socketURL: String {get} - var secure: Bool {get} - func didError(reason: AnyObject) func engineDidClose(reason: String) func parseSocketMessage(msg: String) diff --git a/SocketIOClientSwift/SocketEngineSpec.swift b/SocketIOClientSwift/SocketEngineSpec.swift index 92ed236..4387aa4 100644 --- a/SocketIOClientSwift/SocketEngineSpec.swift +++ b/SocketIOClientSwift/SocketEngineSpec.swift @@ -33,7 +33,7 @@ import Foundation var urlPolling: String {get} var urlWebSocket: String {get} - init(client: SocketEngineClient, options: NSDictionary?) + init(client: SocketEngineClient, url: String, options: NSDictionary?) func close() func open(opts: [String: AnyObject]?) diff --git a/SocketIOClientSwift/SocketIOClient.swift b/SocketIOClientSwift/SocketIOClient.swift index c9a7db0..32a0d8e 100644 --- a/SocketIOClientSwift/SocketIOClient.swift +++ b/SocketIOClientSwift/SocketIOClient.swift @@ -91,6 +91,8 @@ public final class SocketIOClient: NSObject, SocketEngineClient { } } + self.options?.insertIgnore(.Path("/socket.io")) + super.init() } @@ -111,7 +113,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient { private func addEngine() -> SocketEngine { Logger.log("Adding engine", type: logType) - let newEngine = SocketEngine(client: self, options: options ?? []) + let newEngine = SocketEngine(client: self, url: socketURL, options: options ?? []) engine = newEngine return newEngine diff --git a/SocketIOClientSwift/SocketIOClientOption.swift b/SocketIOClientSwift/SocketIOClientOption.swift index 7909d96..e75d5dd 100644 --- a/SocketIOClientSwift/SocketIOClientOption.swift +++ b/SocketIOClientSwift/SocketIOClientOption.swift @@ -24,21 +24,24 @@ import Foundation -public enum SocketIOClientOption: CustomStringConvertible, Hashable { +protocol ClientOptions {} + +public enum SocketIOClientOption: CustomStringConvertible, Hashable, ClientOptions { case ConnectParams([String: AnyObject]) + case Cookies([NSHTTPCookie]) + case ExtraHeaders([String: String]) + case ForcePolling(Bool) + case ForceWebsockets(Bool) + case HandleQueue(dispatch_queue_t) + case Log(Bool) + case Logger(SocketLogger) + case Nsp(String) + case Path(String) case Reconnects(Bool) case ReconnectAttempts(Int) case ReconnectWait(Int) - case ForcePolling(Bool) - case ForceWebsockets(Bool) - case Nsp(String) - case Cookies([NSHTTPCookie]) - case Log(Bool) - case Logger(SocketLogger) + case Secure(Bool) case SessionDelegate(NSURLSessionDelegate) - case Path(String) - case ExtraHeaders([String: String]) - case HandleQueue(dispatch_queue_t) case VoipEnabled(Bool) public var description: String { @@ -85,6 +88,8 @@ public enum SocketIOClientOption: CustomStringConvertible, Hashable { return .HandleQueue(value as! dispatch_queue_t) case "voipEnabled" where value is Bool: return .VoipEnabled(value as! Bool) + case "secure" where value is Bool: + return .Secure(value as! Bool) default: return nil } @@ -120,3 +125,17 @@ public enum SocketIOClientOption: CustomStringConvertible, Hashable { public func ==(lhs: SocketIOClientOption, rhs: SocketIOClientOption) -> Bool { return lhs.description == rhs.description } + +extension Set where Element: ClientOptions { + mutating func insertIgnore(element: Element) { + let (insertType, _) = Mirror(reflecting: element).children.first! + for item in self { + let (name, _) = Mirror(reflecting: item).children.first! + if insertType == name { + return + } + } + + self.insert(element) + } +}