@@ -29,6 +29,11 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
2929 public let handleQueue = dispatch_queue_create ( " com.socketio.engineHandleQueue " , DISPATCH_QUEUE_SERIAL)
3030 public let parseQueue = dispatch_queue_create ( " com.socketio.engineParseQueue " , DISPATCH_QUEUE_SERIAL)
3131
32+ public var connectParams : [ String : AnyObject ] ? {
33+ didSet {
34+ ( urlPolling, urlWebSocket) = createURLs ( )
35+ }
36+ }
3237 public var postWait = [ String] ( )
3338 public var waitingForPoll = false
3439 public var waitingForPost = false
@@ -46,9 +51,9 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
4651 public private( set) var probing = false
4752 public private( set) var session : NSURLSession ?
4853 public private( set) var sid = " "
49- public private( set) var socketPath = " /engine.io "
50- public private( set) var urlPolling = " "
51- public private( set) var urlWebSocket = " "
54+ public private( set) var socketPath = " /engine.io/ "
55+ public private( set) var urlPolling = NSURL ( )
56+ public private( set) var urlWebSocket = NSURL ( )
5257 public private( set) var websocket = false
5358 public private( set) var ws : WebSocket ?
5459
@@ -59,11 +64,9 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
5964 private typealias Probe = ( msg: String , type: SocketEnginePacketType , data: [ NSData ] )
6065 private typealias ProbeWaitQueue = [ Probe ]
6166
62- private let allowedCharacterSet = NSCharacterSet ( charactersInString: " !*'();:@&=+$,/?%#[] \" {} " ) . invertedSet
6367 private let logType = " SocketEngine "
64- private let url : String
68+ private let url : NSURL
6569
66- private var connectParams : [ String : AnyObject ] ?
6770 private var pingInterval : Double ?
6871 private var pingTimeout = 0.0 {
6972 didSet {
@@ -76,13 +79,15 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
7679 private var secure = false
7780 private var selfSigned = false
7881 private var voipEnabled = false
79-
80- public init ( client: SocketEngineClient , url: String , options: Set < SocketIOClientOption > ) {
82+
83+ public init ( client: SocketEngineClient , url: NSURL , options: Set < SocketIOClientOption > ) {
8184 self . client = client
8285 self . url = url
83-
86+
8487 for option in options {
8588 switch option {
89+ case let . ConnectParams( params) :
90+ connectParams = params
8691 case let . SessionDelegate( delegate) :
8792 sessionDelegate = delegate
8893 case let . ForcePolling( force) :
@@ -105,11 +110,26 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
105110 continue
106111 }
107112 }
113+
114+ super. init ( )
115+
116+ ( urlPolling, urlWebSocket) = createURLs ( )
117+ }
118+
119+ public convenience init ( client: SocketEngineClient , url: NSURL , options: NSDictionary ? ) {
120+ self . init ( client: client, url: url, options: options? . toSocketOptionsSet ( ) ?? [ ] )
108121 }
109122
110- public convenience init ( client: SocketEngineClient , url: String , options: NSDictionary ? ) {
111- self . init ( client: client, url: url,
112- options: options? . toSocketOptionsSet ( ) ?? [ ] )
123+ @available ( * , deprecated= 5.3 )
124+ public convenience init ( client: SocketEngineClient , urlString: String , options: Set < SocketIOClientOption > ) {
125+ guard let url = NSURL ( string: urlString) else { fatalError ( " Incorrect url " ) }
126+ self . init ( client: client, url: url, options: options)
127+ }
128+
129+ @available ( * , deprecated= 5.3 )
130+ public convenience init ( client: SocketEngineClient , urlString: String , options: NSDictionary ? ) {
131+ guard let url = NSURL ( string: urlString) else { fatalError ( " Incorrect url " ) }
132+ self . init ( client: client, url: url, options: options? . toSocketOptionsSet ( ) ?? [ ] )
113133 }
114134
115135 deinit {
@@ -131,7 +151,7 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
131151 switch code {
132152 case 0 : // Unknown transport
133153 didError ( error)
134- case 1 : // Unknown sid. clear and retry connect
154+ case 1 : // Unknown sid.
135155 didError ( error)
136156 case 2 : // Bad handshake request
137157 didError ( error)
@@ -188,49 +208,45 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
188208 }
189209 }
190210
191- private func createURLs( params : [ String : AnyObject ] ? ) -> ( String , String ) {
211+ private func createURLs( ) -> ( NSURL , NSURL ) {
192212 if client == nil {
193- return ( " " , " " )
213+ return ( NSURL ( ) , NSURL ( ) )
194214 }
195215
196- let socketURL = " \( url) \( socketPath) /?transport= "
197- var urlPolling : String
198- var urlWebSocket : String
216+ let urlPolling = NSURLComponents ( string: url. absoluteString) !
217+ let urlWebSocket = NSURLComponents ( string: url. absoluteString) !
218+ var queryString = " "
219+
220+ urlWebSocket. path = socketPath
221+ urlPolling. path = socketPath
222+ urlWebSocket. query = " transport=websocket "
223+ urlPolling. query = " transport=polling&b64=1 "
199224
200225 if secure {
201- urlPolling = " https:// " + socketURL + " polling "
202- urlWebSocket = " wss:// " + socketURL + " websocket "
226+ urlPolling. scheme = " https "
227+ urlWebSocket. scheme = " wss "
203228 } else {
204- urlPolling = " http:// " + socketURL + " polling "
205- urlWebSocket = " ws:// " + socketURL + " websocket "
229+ urlPolling. scheme = " http "
230+ urlWebSocket. scheme = " ws "
206231 }
207232
208- if params != nil {
209- for (key, value) in params! {
210- let keyEsc = key. stringByAddingPercentEncodingWithAllowedCharacters (
211- allowedCharacterSet) !
212- urlPolling += " & \( keyEsc) = "
213- urlWebSocket += " & \( keyEsc) = "
214-
215- if value is String {
216- let valueEsc = ( value as! String ) . stringByAddingPercentEncodingWithAllowedCharacters (
217- allowedCharacterSet) !
218- urlPolling += " \( valueEsc) "
219- urlWebSocket += " \( valueEsc) "
220- } else {
221- urlPolling += " \( value) "
222- urlWebSocket += " \( value) "
223- }
233+ if connectParams != nil {
234+ for (key, value) in connectParams! {
235+ queryString += " & \( key) = \( value) "
224236 }
225237 }
226238
227- return ( urlPolling, urlWebSocket)
239+ urlWebSocket. query = urlWebSocket. query! + queryString
240+ urlPolling. query = urlPolling. query! + queryString
241+
242+ return ( urlPolling. URL!, urlWebSocket. URL!)
228243 }
229244
230245 private func createWebsocketAndConnect( ) {
231- let wsUrl = urlWebSocket + ( sid == " " ? " " : " &sid= \( sid) " )
232-
233- ws = WebSocket ( url: NSURL ( string: wsUrl) !)
246+ let component = NSURLComponents ( URL: urlWebSocket, resolvingAgainstBaseURL: false ) !
247+ component. query = component. query! + ( sid == " " ? " " : " &sid= \( sid) " )
248+
249+ ws = WebSocket ( url: component. URL!)
234250
235251 if cookies != nil {
236252 let headers = NSHTTPCookie . requestHeaderFieldsWithCookies ( cookies!)
@@ -363,30 +379,26 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
363379 }
364380 }
365381
366- public func open( opts : [ String : AnyObject ] ? ) {
382+ public func open( ) {
367383 if connected {
368384 DefaultSocketLogger . Logger. error ( " Engine tried opening while connected. This is probably a programming error. "
369385 + " Abandoning open attempt " , type: logType)
370386 return
371387 }
372388
373- connectParams = opts
374-
375389 DefaultSocketLogger . Logger. log ( " Starting engine " , type: logType)
376390 DefaultSocketLogger . Logger. log ( " Handshaking " , type: logType)
377391
378392 resetEngine ( )
379393
380- ( urlPolling, urlWebSocket) = createURLs ( opts)
381-
382394 if forceWebsockets {
383395 polling = false
384396 websocket = true
385397 createWebsocketAndConnect ( )
386398 return
387399 }
388400
389- let reqPolling = NSMutableURLRequest ( URL: NSURL ( string : urlPolling + " &b64=1 " ) ! )
401+ let reqPolling = NSMutableURLRequest ( URL: urlPolling)
390402
391403 if cookies != nil {
392404 let headers = NSHTTPCookie . requestHeaderFieldsWithCookies ( cookies!)
0 commit comments