further decouple engine and client

This commit is contained in:
Erik 2015-10-29 18:25:45 -04:00
parent 9deb5a06a6
commit 47fd0e386f
7 changed files with 53 additions and 31 deletions

View File

@ -15,7 +15,7 @@
<BuildableReference <BuildableReference
BuildableIdentifier = "primary" BuildableIdentifier = "primary"
BlueprintIdentifier = "576349FA1BD9B46A00E19CD7" BlueprintIdentifier = "576349FA1BD9B46A00E19CD7"
BuildableName = "SocketIO-tvOS.framework" BuildableName = "SocketIO.framework"
BlueprintName = "SocketIO-tvOS" BlueprintName = "SocketIO-tvOS"
ReferencedContainer = "container:Socket.IO-Client-Swift.xcodeproj"> ReferencedContainer = "container:Socket.IO-Client-Swift.xcodeproj">
</BuildableReference> </BuildableReference>
@ -57,7 +57,7 @@
<BuildableReference <BuildableReference
BuildableIdentifier = "primary" BuildableIdentifier = "primary"
BlueprintIdentifier = "576349FA1BD9B46A00E19CD7" BlueprintIdentifier = "576349FA1BD9B46A00E19CD7"
BuildableName = "SocketIO-tvOS.framework" BuildableName = "SocketIO.framework"
BlueprintName = "SocketIO-tvOS" BlueprintName = "SocketIO-tvOS"
ReferencedContainer = "container:Socket.IO-Client-Swift.xcodeproj"> ReferencedContainer = "container:Socket.IO-Client-Swift.xcodeproj">
</BuildableReference> </BuildableReference>
@ -79,7 +79,7 @@
<BuildableReference <BuildableReference
BuildableIdentifier = "primary" BuildableIdentifier = "primary"
BlueprintIdentifier = "576349FA1BD9B46A00E19CD7" BlueprintIdentifier = "576349FA1BD9B46A00E19CD7"
BuildableName = "SocketIO-tvOS.framework" BuildableName = "SocketIO.framework"
BlueprintName = "SocketIO-tvOS" BlueprintName = "SocketIO-tvOS"
ReferencedContainer = "container:Socket.IO-Client-Swift.xcodeproj"> ReferencedContainer = "container:Socket.IO-Client-Swift.xcodeproj">
</BuildableReference> </BuildableReference>
@ -97,7 +97,7 @@
<BuildableReference <BuildableReference
BuildableIdentifier = "primary" BuildableIdentifier = "primary"
BlueprintIdentifier = "576349FA1BD9B46A00E19CD7" BlueprintIdentifier = "576349FA1BD9B46A00E19CD7"
BuildableName = "SocketIO-tvOS.framework" BuildableName = "SocketIO.framework"
BlueprintName = "SocketIO-tvOS" BlueprintName = "SocketIO-tvOS"
ReferencedContainer = "container:Socket.IO-Client-Swift.xcodeproj"> ReferencedContainer = "container:Socket.IO-Client-Swift.xcodeproj">
</BuildableReference> </BuildableReference>

View File

@ -15,7 +15,7 @@ class SocketEngineTest: XCTestCase {
override func setUp() { override func setUp() {
super.setUp() super.setUp()
client = SocketIOClient(socketURL: "") client = SocketIOClient(socketURL: "")
engine = SocketEngine(client: client, options: nil) engine = SocketEngine(client: client, url: "", options: nil)
client.setTestable() client.setTestable()
} }

View File

@ -27,7 +27,7 @@ import Foundation
public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate { public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate {
public private(set) var sid = "" public private(set) var sid = ""
public private(set) var cookies: [NSHTTPCookie]? 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 urlPolling = ""
public private(set) var urlWebSocket = "" public private(set) var urlWebSocket = ""
public private(set) var ws: WebSocket? 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 handleQueue = dispatch_queue_create("com.socketio.engineHandleQueue", DISPATCH_QUEUE_SERIAL)
private let logType = "SocketEngine" private let logType = "SocketEngine"
private let parseQueue = dispatch_queue_create("com.socketio.engineParseQueue", DISPATCH_QUEUE_SERIAL) private let parseQueue = dispatch_queue_create("com.socketio.engineParseQueue", DISPATCH_QUEUE_SERIAL)
private let url: String
private let workQueue = NSOperationQueue() private let workQueue = NSOperationQueue()
private var closed = false private var closed = false
@ -62,6 +63,7 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate {
private var postWait = [String]() private var postWait = [String]()
private var probing = false private var probing = false
private var probeWait = ProbeWaitQueue() private var probeWait = ProbeWaitQueue()
private var secure = false
private var session: NSURLSession! private var session: NSURLSession!
private var voipEnabled = false private var voipEnabled = false
private var waitingForPoll = false private var waitingForPoll = false
@ -72,8 +74,9 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate {
private(set) var polling = true private(set) var polling = true
private(set) var websocket = false private(set) var websocket = false
public init(client: SocketEngineClient, options: Set<SocketIOClientOption>) { public init(client: SocketEngineClient, url: String, options: Set<SocketIOClientOption>) {
self.client = client self.client = client
self.url = url
for option in options { for option in options {
switch option { switch option {
@ -93,6 +96,8 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate {
extraHeaders = headers extraHeaders = headers
case .VoipEnabled(let enable): case .VoipEnabled(let enable):
voipEnabled = enable voipEnabled = enable
case .Secure(let secure):
self.secure = secure
default: default:
continue continue
} }
@ -105,8 +110,8 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate {
} }
} }
public convenience init(client: SocketEngineClient, options: NSDictionary?) { public convenience init(client: SocketEngineClient, url: String, options: NSDictionary?) {
self.init(client: client, self.init(client: client, url: url,
options: SocketIOClientOption.NSDictionaryToSocketOptionsSet(options ?? [:])) options: SocketIOClientOption.NSDictionaryToSocketOptionsSet(options ?? [:]))
} }
@ -167,17 +172,16 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate {
return ("", "") return ("", "")
} }
let path = socketPath == "" ? "/socket.io" : socketPath let socketURL = "\(url)\(socketPath)/?transport="
let url = "\(client!.socketURL)\(path)/?transport="
var urlPolling: String var urlPolling: String
var urlWebSocket: String var urlWebSocket: String
if client!.secure { if secure {
urlPolling = "https://" + url + "polling" urlPolling = "https://" + socketURL + "polling"
urlWebSocket = "wss://" + url + "websocket" urlWebSocket = "wss://" + socketURL + "websocket"
} else { } else {
urlPolling = "http://" + url + "polling" urlPolling = "http://" + socketURL + "polling"
urlWebSocket = "ws://" + url + "websocket" urlWebSocket = "ws://" + socketURL + "websocket"
} }
if params != nil { if params != nil {

View File

@ -26,9 +26,6 @@
import Foundation import Foundation
@objc public protocol SocketEngineClient { @objc public protocol SocketEngineClient {
var socketURL: String {get}
var secure: Bool {get}
func didError(reason: AnyObject) func didError(reason: AnyObject)
func engineDidClose(reason: String) func engineDidClose(reason: String)
func parseSocketMessage(msg: String) func parseSocketMessage(msg: String)

View File

@ -33,7 +33,7 @@ import Foundation
var urlPolling: String {get} var urlPolling: String {get}
var urlWebSocket: String {get} var urlWebSocket: String {get}
init(client: SocketEngineClient, options: NSDictionary?) init(client: SocketEngineClient, url: String, options: NSDictionary?)
func close() func close()
func open(opts: [String: AnyObject]?) func open(opts: [String: AnyObject]?)

View File

@ -91,6 +91,8 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
} }
} }
self.options?.insertIgnore(.Path("/socket.io"))
super.init() super.init()
} }
@ -111,7 +113,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
private func addEngine() -> SocketEngine { private func addEngine() -> SocketEngine {
Logger.log("Adding engine", type: logType) Logger.log("Adding engine", type: logType)
let newEngine = SocketEngine(client: self, options: options ?? []) let newEngine = SocketEngine(client: self, url: socketURL, options: options ?? [])
engine = newEngine engine = newEngine
return newEngine return newEngine

View File

@ -24,21 +24,24 @@
import Foundation import Foundation
public enum SocketIOClientOption: CustomStringConvertible, Hashable { protocol ClientOptions {}
public enum SocketIOClientOption: CustomStringConvertible, Hashable, ClientOptions {
case ConnectParams([String: AnyObject]) 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 Reconnects(Bool)
case ReconnectAttempts(Int) case ReconnectAttempts(Int)
case ReconnectWait(Int) case ReconnectWait(Int)
case ForcePolling(Bool) case Secure(Bool)
case ForceWebsockets(Bool)
case Nsp(String)
case Cookies([NSHTTPCookie])
case Log(Bool)
case Logger(SocketLogger)
case SessionDelegate(NSURLSessionDelegate) case SessionDelegate(NSURLSessionDelegate)
case Path(String)
case ExtraHeaders([String: String])
case HandleQueue(dispatch_queue_t)
case VoipEnabled(Bool) case VoipEnabled(Bool)
public var description: String { public var description: String {
@ -85,6 +88,8 @@ public enum SocketIOClientOption: CustomStringConvertible, Hashable {
return .HandleQueue(value as! dispatch_queue_t) return .HandleQueue(value as! dispatch_queue_t)
case "voipEnabled" where value is Bool: case "voipEnabled" where value is Bool:
return .VoipEnabled(value as! Bool) return .VoipEnabled(value as! Bool)
case "secure" where value is Bool:
return .Secure(value as! Bool)
default: default:
return nil return nil
} }
@ -120,3 +125,17 @@ public enum SocketIOClientOption: CustomStringConvertible, Hashable {
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 {
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)
}
}