Skip to content

Commit d912796

Browse files
committed
engine websocket url to nsurl
1 parent c9ac69f commit d912796

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

Source/SocketEngine.swift

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
4646
public private(set) var probing = false
4747
public private(set) var session: NSURLSession?
4848
public private(set) var sid = ""
49-
public private(set) var socketPath = "/engine.io"
49+
public private(set) var socketPath = "/engine.io/"
5050
public private(set) var urlPolling = ""
51-
public private(set) var urlWebSocket = ""
51+
public private(set) var urlWebSocket = NSURL()
5252
public private(set) var websocket = false
5353
public private(set) var ws: WebSocket?
5454

@@ -199,9 +199,9 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
199199
}
200200
}
201201

202-
private func createURLs(params: [String: AnyObject]?) -> (String, String) {
202+
private func createURLs(params: [String: AnyObject]?) -> (String, NSURL) {
203203
if client == nil {
204-
return ("", "")
204+
return ("", NSURL())
205205
}
206206

207207
let absURL = url.absoluteString["https?://"] <~ ""
@@ -215,42 +215,47 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
215215

216216
let socketURL = "\(baseURL)\(socketPath)/?transport="
217217
var urlPolling: String
218-
var urlWebSocket: String
218+
var queryString = "transport=websocket"
219+
let urlWebSocket = NSURLComponents(string: url.absoluteString)!
220+
221+
urlWebSocket.path = socketPath
219222

220223
if secure {
221224
urlPolling = "https://" + socketURL + "polling"
222-
urlWebSocket = "wss://" + socketURL + "websocket"
225+
urlWebSocket.scheme = "wss"
223226
} else {
224227
urlPolling = "http://" + socketURL + "polling"
225-
urlWebSocket = "ws://" + socketURL + "websocket"
228+
urlWebSocket.scheme = "ws"
226229
}
227230

228231
if params != nil {
229232
for (key, value) in params! {
230233
let keyEsc = key.stringByAddingPercentEncodingWithAllowedCharacters(
231234
allowedCharacterSet)!
232235
urlPolling += "&\(keyEsc)="
233-
urlWebSocket += "&\(keyEsc)="
236+
queryString += "&\(keyEsc)="
234237

235238
if value is String {
236239
let valueEsc = (value as! String).stringByAddingPercentEncodingWithAllowedCharacters(
237240
allowedCharacterSet)!
238-
urlPolling += "\(valueEsc)"
239-
urlWebSocket += "\(valueEsc)"
241+
urlPolling += String(valueEsc)
242+
queryString += String(value)
240243
} else {
241-
urlPolling += "\(value)"
242-
urlWebSocket += "\(value)"
244+
urlPolling += String(value)
245+
queryString += String(value)
243246
}
244247
}
245248
}
246249

247-
return (urlPolling, urlWebSocket)
250+
urlWebSocket.query = queryString
251+
return (urlPolling, urlWebSocket.URL!)
248252
}
249253

250254
private func createWebsocketAndConnect() {
251-
let wsUrl = urlWebSocket + (sid == "" ? "" : "&sid=\(sid)")
252-
253-
ws = WebSocket(url: NSURL(string: wsUrl)!)
255+
let component = NSURLComponents(URL: urlWebSocket, resolvingAgainstBaseURL: false)!
256+
component.query = component.query! + (sid == "" ? "" : "&sid=\(sid)")
257+
258+
ws = WebSocket(url: component.URL!)
254259

255260
if cookies != nil {
256261
let headers = NSHTTPCookie.requestHeaderFieldsWithCookies(cookies!)

Source/SocketEngineSpec.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import Foundation
4343
var sid: String { get }
4444
var socketPath: String { get }
4545
var urlPolling: String { get }
46-
var urlWebSocket: String { get }
46+
var urlWebSocket: NSURL { get }
4747
var websocket: Bool { get }
4848

4949
init(client: SocketEngineClient, url: NSURL, options: NSDictionary?)

Source/SocketIOClient.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable
9292
}
9393
}
9494

95-
self.options.insertIgnore(.Path("/socket.io"))
95+
self.options.insertIgnore(.Path("/socket.io/"))
9696

9797
super.init()
9898
}

0 commit comments

Comments
 (0)