Skip to content

Commit 47fd0e3

Browse files
committed
further decouple engine and client
1 parent 9deb5a0 commit 47fd0e3

File tree

7 files changed

+52
-30
lines changed

7 files changed

+52
-30
lines changed

Socket.IO-Client-Swift.xcodeproj/xcshareddata/xcschemes/SocketIO-tvOS.xcscheme

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<BuildableReference
1616
BuildableIdentifier = "primary"
1717
BlueprintIdentifier = "576349FA1BD9B46A00E19CD7"
18-
BuildableName = "SocketIO-tvOS.framework"
18+
BuildableName = "SocketIO.framework"
1919
BlueprintName = "SocketIO-tvOS"
2020
ReferencedContainer = "container:Socket.IO-Client-Swift.xcodeproj">
2121
</BuildableReference>
@@ -57,7 +57,7 @@
5757
<BuildableReference
5858
BuildableIdentifier = "primary"
5959
BlueprintIdentifier = "576349FA1BD9B46A00E19CD7"
60-
BuildableName = "SocketIO-tvOS.framework"
60+
BuildableName = "SocketIO.framework"
6161
BlueprintName = "SocketIO-tvOS"
6262
ReferencedContainer = "container:Socket.IO-Client-Swift.xcodeproj">
6363
</BuildableReference>
@@ -79,7 +79,7 @@
7979
<BuildableReference
8080
BuildableIdentifier = "primary"
8181
BlueprintIdentifier = "576349FA1BD9B46A00E19CD7"
82-
BuildableName = "SocketIO-tvOS.framework"
82+
BuildableName = "SocketIO.framework"
8383
BlueprintName = "SocketIO-tvOS"
8484
ReferencedContainer = "container:Socket.IO-Client-Swift.xcodeproj">
8585
</BuildableReference>
@@ -97,7 +97,7 @@
9797
<BuildableReference
9898
BuildableIdentifier = "primary"
9999
BlueprintIdentifier = "576349FA1BD9B46A00E19CD7"
100-
BuildableName = "SocketIO-tvOS.framework"
100+
BuildableName = "SocketIO.framework"
101101
BlueprintName = "SocketIO-tvOS"
102102
ReferencedContainer = "container:Socket.IO-Client-Swift.xcodeproj">
103103
</BuildableReference>

SocketIO-MacTests/SocketEngineTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class SocketEngineTest: XCTestCase {
1515
override func setUp() {
1616
super.setUp()
1717
client = SocketIOClient(socketURL: "")
18-
engine = SocketEngine(client: client, options: nil)
18+
engine = SocketEngine(client: client, url: "", options: nil)
1919

2020
client.setTestable()
2121
}

SocketIOClientSwift/SocketEngine.swift

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import Foundation
2727
public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate {
2828
public private(set) var sid = ""
2929
public private(set) var cookies: [NSHTTPCookie]?
30-
public private(set) var socketPath = ""
30+
public private(set) var socketPath = "/engine.io"
3131
public private(set) var urlPolling = ""
3232
public private(set) var urlWebSocket = ""
3333
public private(set) var ws: WebSocket?
@@ -42,6 +42,7 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate {
4242
private let handleQueue = dispatch_queue_create("com.socketio.engineHandleQueue", DISPATCH_QUEUE_SERIAL)
4343
private let logType = "SocketEngine"
4444
private let parseQueue = dispatch_queue_create("com.socketio.engineParseQueue", DISPATCH_QUEUE_SERIAL)
45+
private let url: String
4546
private let workQueue = NSOperationQueue()
4647

4748
private var closed = false
@@ -62,6 +63,7 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate {
6263
private var postWait = [String]()
6364
private var probing = false
6465
private var probeWait = ProbeWaitQueue()
66+
private var secure = false
6567
private var session: NSURLSession!
6668
private var voipEnabled = false
6769
private var waitingForPoll = false
@@ -72,8 +74,9 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate {
7274
private(set) var polling = true
7375
private(set) var websocket = false
7476

75-
public init(client: SocketEngineClient, options: Set<SocketIOClientOption>) {
77+
public init(client: SocketEngineClient, url: String, options: Set<SocketIOClientOption>) {
7678
self.client = client
79+
self.url = url
7780

7881
for option in options {
7982
switch option {
@@ -93,6 +96,8 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate {
9396
extraHeaders = headers
9497
case .VoipEnabled(let enable):
9598
voipEnabled = enable
99+
case .Secure(let secure):
100+
self.secure = secure
96101
default:
97102
continue
98103
}
@@ -105,8 +110,8 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate {
105110
}
106111
}
107112

108-
public convenience init(client: SocketEngineClient, options: NSDictionary?) {
109-
self.init(client: client,
113+
public convenience init(client: SocketEngineClient, url: String, options: NSDictionary?) {
114+
self.init(client: client, url: url,
110115
options: SocketIOClientOption.NSDictionaryToSocketOptionsSet(options ?? [:]))
111116
}
112117

@@ -167,17 +172,16 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate {
167172
return ("", "")
168173
}
169174

170-
let path = socketPath == "" ? "/socket.io" : socketPath
171-
let url = "\(client!.socketURL)\(path)/?transport="
175+
let socketURL = "\(url)\(socketPath)/?transport="
172176
var urlPolling: String
173177
var urlWebSocket: String
174178

175-
if client!.secure {
176-
urlPolling = "https://" + url + "polling"
177-
urlWebSocket = "wss://" + url + "websocket"
179+
if secure {
180+
urlPolling = "https://" + socketURL + "polling"
181+
urlWebSocket = "wss://" + socketURL + "websocket"
178182
} else {
179-
urlPolling = "http://" + url + "polling"
180-
urlWebSocket = "ws://" + url + "websocket"
183+
urlPolling = "http://" + socketURL + "polling"
184+
urlWebSocket = "ws://" + socketURL + "websocket"
181185
}
182186

183187
if params != nil {

SocketIOClientSwift/SocketEngineClient.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
import Foundation
2727

2828
@objc public protocol SocketEngineClient {
29-
var socketURL: String {get}
30-
var secure: Bool {get}
31-
3229
func didError(reason: AnyObject)
3330
func engineDidClose(reason: String)
3431
func parseSocketMessage(msg: String)

SocketIOClientSwift/SocketEngineSpec.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import Foundation
3333
var urlPolling: String {get}
3434
var urlWebSocket: String {get}
3535

36-
init(client: SocketEngineClient, options: NSDictionary?)
36+
init(client: SocketEngineClient, url: String, options: NSDictionary?)
3737

3838
func close()
3939
func open(opts: [String: AnyObject]?)

SocketIOClientSwift/SocketIOClient.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
9191
}
9292
}
9393

94+
self.options?.insertIgnore(.Path("/socket.io"))
95+
9496
super.init()
9597
}
9698

@@ -111,7 +113,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
111113
private func addEngine() -> SocketEngine {
112114
Logger.log("Adding engine", type: logType)
113115

114-
let newEngine = SocketEngine(client: self, options: options ?? [])
116+
let newEngine = SocketEngine(client: self, url: socketURL, options: options ?? [])
115117

116118
engine = newEngine
117119
return newEngine

SocketIOClientSwift/SocketIOClientOption.swift

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,24 @@
2424

2525
import Foundation
2626

27-
public enum SocketIOClientOption: CustomStringConvertible, Hashable {
27+
protocol ClientOptions {}
28+
29+
public enum SocketIOClientOption: CustomStringConvertible, Hashable, ClientOptions {
2830
case ConnectParams([String: AnyObject])
29-
case Reconnects(Bool)
30-
case ReconnectAttempts(Int)
31-
case ReconnectWait(Int)
31+
case Cookies([NSHTTPCookie])
32+
case ExtraHeaders([String: String])
3233
case ForcePolling(Bool)
3334
case ForceWebsockets(Bool)
34-
case Nsp(String)
35-
case Cookies([NSHTTPCookie])
35+
case HandleQueue(dispatch_queue_t)
3636
case Log(Bool)
3737
case Logger(SocketLogger)
38-
case SessionDelegate(NSURLSessionDelegate)
38+
case Nsp(String)
3939
case Path(String)
40-
case ExtraHeaders([String: String])
41-
case HandleQueue(dispatch_queue_t)
40+
case Reconnects(Bool)
41+
case ReconnectAttempts(Int)
42+
case ReconnectWait(Int)
43+
case Secure(Bool)
44+
case SessionDelegate(NSURLSessionDelegate)
4245
case VoipEnabled(Bool)
4346

4447
public var description: String {
@@ -85,6 +88,8 @@ public enum SocketIOClientOption: CustomStringConvertible, Hashable {
8588
return .HandleQueue(value as! dispatch_queue_t)
8689
case "voipEnabled" where value is Bool:
8790
return .VoipEnabled(value as! Bool)
91+
case "secure" where value is Bool:
92+
return .Secure(value as! Bool)
8893
default:
8994
return nil
9095
}
@@ -120,3 +125,17 @@ public enum SocketIOClientOption: CustomStringConvertible, Hashable {
120125
public func ==(lhs: SocketIOClientOption, rhs: SocketIOClientOption) -> Bool {
121126
return lhs.description == rhs.description
122127
}
128+
129+
extension Set where Element: ClientOptions {
130+
mutating func insertIgnore(element: Element) {
131+
let (insertType, _) = Mirror(reflecting: element).children.first!
132+
for item in self {
133+
let (name, _) = Mirror(reflecting: item).children.first!
134+
if insertType == name {
135+
return
136+
}
137+
}
138+
139+
self.insert(element)
140+
}
141+
}

0 commit comments

Comments
 (0)