further decouple engine and client
This commit is contained in:
parent
9deb5a06a6
commit
47fd0e386f
@ -15,7 +15,7 @@
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "576349FA1BD9B46A00E19CD7"
|
||||
BuildableName = "SocketIO-tvOS.framework"
|
||||
BuildableName = "SocketIO.framework"
|
||||
BlueprintName = "SocketIO-tvOS"
|
||||
ReferencedContainer = "container:Socket.IO-Client-Swift.xcodeproj">
|
||||
</BuildableReference>
|
||||
@ -57,7 +57,7 @@
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "576349FA1BD9B46A00E19CD7"
|
||||
BuildableName = "SocketIO-tvOS.framework"
|
||||
BuildableName = "SocketIO.framework"
|
||||
BlueprintName = "SocketIO-tvOS"
|
||||
ReferencedContainer = "container:Socket.IO-Client-Swift.xcodeproj">
|
||||
</BuildableReference>
|
||||
@ -79,7 +79,7 @@
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "576349FA1BD9B46A00E19CD7"
|
||||
BuildableName = "SocketIO-tvOS.framework"
|
||||
BuildableName = "SocketIO.framework"
|
||||
BlueprintName = "SocketIO-tvOS"
|
||||
ReferencedContainer = "container:Socket.IO-Client-Swift.xcodeproj">
|
||||
</BuildableReference>
|
||||
@ -97,7 +97,7 @@
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "576349FA1BD9B46A00E19CD7"
|
||||
BuildableName = "SocketIO-tvOS.framework"
|
||||
BuildableName = "SocketIO.framework"
|
||||
BlueprintName = "SocketIO-tvOS"
|
||||
ReferencedContainer = "container:Socket.IO-Client-Swift.xcodeproj">
|
||||
</BuildableReference>
|
||||
|
||||
@ -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()
|
||||
}
|
||||
|
||||
@ -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<SocketIOClientOption>) {
|
||||
public init(client: SocketEngineClient, url: String, options: Set<SocketIOClientOption>) {
|
||||
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 {
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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]?)
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user