Skip to content

Commit c9ac69f

Browse files
committed
first pass at #288
1 parent cc7d96e commit c9ac69f

File tree

6 files changed

+59
-33
lines changed

6 files changed

+59
-33
lines changed

SocketIO-MacTests/SocketEngineTest.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class SocketEngineTest: XCTestCase {
1515

1616
override func setUp() {
1717
super.setUp()
18-
client = SocketIOClient(socketURL: "")
19-
engine = SocketEngine(client: client, url: "", options: nil)
18+
client = SocketIOClient(socketURL: NSURL())
19+
engine = SocketEngine(client: client, url: NSURL(), options: nil)
2020

2121
client.setTestable()
2222
}

SocketIO-MacTests/SocketParserTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import XCTest
1010
@testable import SocketIOClientSwift
1111

1212
class SocketParserTest: XCTestCase {
13-
let testSocket = SocketIOClient(socketURL: "")
13+
let testSocket = SocketIOClient(socketURL: NSURL())
1414

1515
//Format key: message; namespace-data-binary-id
1616
static let packetTypes: Dictionary<String, (String, [AnyObject], [NSData], Int)> = [

SocketIO-MacTests/SocketSideEffectTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class SocketSideEffectTest: XCTestCase {
1616

1717
override func setUp() {
1818
super.setUp()
19-
socket = SocketIOClient(socketURL: "")
19+
socket = SocketIOClient(socketURL: NSURL())
2020
socket.setTestable()
2121
}
2222

Source/SocketEngine.swift

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
6161

6262
private let allowedCharacterSet = NSCharacterSet(charactersInString: "!*'();:@&=+$,/?%#[]\" {}").invertedSet
6363
private let logType = "SocketEngine"
64-
private let url: String
64+
private let url: NSURL
6565

6666
private var connectParams: [String: AnyObject]?
6767
private var pingInterval: Double?
@@ -76,11 +76,11 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
7676
private var secure = false
7777
private var selfSigned = false
7878
private var voipEnabled = false
79-
80-
public init(client: SocketEngineClient, url: String, options: Set<SocketIOClientOption>) {
79+
80+
public init(client: SocketEngineClient, url: NSURL, options: Set<SocketIOClientOption>) {
8181
self.client = client
8282
self.url = url
83-
83+
8484
for option in options {
8585
switch option {
8686
case let .SessionDelegate(delegate):
@@ -107,9 +107,20 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
107107
}
108108
}
109109

110-
public convenience init(client: SocketEngineClient, url: String, options: NSDictionary?) {
111-
self.init(client: client, url: url,
112-
options: options?.toSocketOptionsSet() ?? [])
110+
public convenience init(client: SocketEngineClient, url: NSURL, options: NSDictionary?) {
111+
self.init(client: client, url: url, options: options?.toSocketOptionsSet() ?? [])
112+
}
113+
114+
@available(*, deprecated=5.3)
115+
public convenience init(client: SocketEngineClient, urlString: String, options: Set<SocketIOClientOption>) {
116+
guard let url = NSURL(string: urlString) else { fatalError("Incorrect url") }
117+
self.init(client: client, url: url, options: options)
118+
}
119+
120+
@available(*, deprecated=5.3)
121+
public convenience init(client: SocketEngineClient, urlString: String, options: NSDictionary?) {
122+
guard let url = NSURL(string: urlString) else { fatalError("Incorrect url") }
123+
self.init(client: client, url: url, options: options?.toSocketOptionsSet() ?? [])
113124
}
114125

115126
deinit {
@@ -193,7 +204,16 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
193204
return ("", "")
194205
}
195206

196-
let socketURL = "\(url)\(socketPath)/?transport="
207+
let absURL = url.absoluteString["https?://"] <~ ""
208+
let baseURL: String
209+
210+
if absURL.hasSuffix("/") {
211+
baseURL = String(absURL.characters.dropLast())
212+
} else {
213+
baseURL = absURL
214+
}
215+
216+
let socketURL = "\(baseURL)\(socketPath)/?transport="
197217
var urlPolling: String
198218
var urlWebSocket: String
199219

Source/SocketEngineSpec.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import Foundation
4646
var urlWebSocket: String { get }
4747
var websocket: Bool { get }
4848

49-
init(client: SocketEngineClient, url: String, options: NSDictionary?)
49+
init(client: SocketEngineClient, url: NSURL, options: NSDictionary?)
5050

5151
func close(reason: String)
5252
func didError(error: String)

Source/SocketIOClient.swift

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import Foundation
2626

2727
public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable {
28-
public let socketURL: String
28+
public let socketURL: NSURL
2929

3030
public private(set) var engine: SocketEngineSpec?
3131
public private(set) var status = SocketIOClientStatus.NotConnected
@@ -55,25 +55,18 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable
5555
private(set) var reconnectAttempts = -1
5656

5757
var waitingData = [SocketPacket]()
58-
58+
5959
/**
6060
Type safe way to create a new SocketIOClient. opts can be omitted
6161
*/
62-
public init(socketURL: String, options: Set<SocketIOClientOption> = []) {
62+
public init(socketURL: NSURL, options: Set<SocketIOClientOption> = []) {
6363
self.options = options
64-
65-
if socketURL.hasPrefix("https://") {
64+
self.socketURL = socketURL
65+
66+
if socketURL.absoluteString.hasPrefix("https://") {
6667
self.options.insertIgnore(.Secure(true))
6768
}
6869

69-
var cleanedURL = socketURL["https?://"] <~ ""
70-
71-
if cleanedURL.hasSuffix("/") {
72-
cleanedURL = String(cleanedURL.characters.dropLast())
73-
}
74-
75-
self.socketURL = cleanedURL
76-
7770
for option in options {
7871
switch option {
7972
case let .ConnectParams(params):
@@ -98,19 +91,32 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable
9891
continue
9992
}
10093
}
101-
94+
10295
self.options.insertIgnore(.Path("/socket.io"))
103-
96+
10497
super.init()
10598
}
106-
99+
107100
/**
108101
Not so type safe way to create a SocketIOClient, meant for Objective-C compatiblity.
109-
If using Swift it's recommended to use `init(var socketURL: String, options: Set<SocketIOClientOption>)`
102+
If using Swift it's recommended to use `init(var socketURL: NSURL, options: Set<SocketIOClientOption>)`
110103
*/
111-
public convenience init(socketURL: String, options: NSDictionary?) {
112-
self.init(socketURL: socketURL,
113-
options: options?.toSocketOptionsSet() ?? [])
104+
public convenience init(socketURL: NSURL, options: NSDictionary?) {
105+
self.init(socketURL: socketURL, options: options?.toSocketOptionsSet() ?? [])
106+
}
107+
108+
/// Please use the NSURL based init
109+
@available(*, deprecated=5.3)
110+
public convenience init(socketURLString: String, options: Set<SocketIOClientOption> = []) {
111+
guard let url = NSURL(string: socketURLString) else { fatalError("Incorrect url") }
112+
self.init(socketURL: url, options: options)
113+
}
114+
115+
/// Please use the NSURL based init
116+
@available(*, deprecated=5.3)
117+
public convenience init(socketURLString: String, options: NSDictionary?) {
118+
guard let url = NSURL(string: socketURLString) else { fatalError("Incorrect url") }
119+
self.init(socketURL: url, options: options?.toSocketOptionsSet() ?? [])
114120
}
115121

116122
deinit {

0 commit comments

Comments
 (0)