@@ -29,6 +29,11 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
29
29
public let handleQueue = dispatch_queue_create ( " com.socketio.engineHandleQueue " , DISPATCH_QUEUE_SERIAL)
30
30
public let parseQueue = dispatch_queue_create ( " com.socketio.engineParseQueue " , DISPATCH_QUEUE_SERIAL)
31
31
32
+ public var connectParams : [ String : AnyObject ] ? {
33
+ didSet {
34
+ ( urlPolling, urlWebSocket) = createURLs ( )
35
+ }
36
+ }
32
37
public var postWait = [ String] ( )
33
38
public var waitingForPoll = false
34
39
public var waitingForPost = false
@@ -46,9 +51,9 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
46
51
public private( set) var probing = false
47
52
public private( set) var session : NSURLSession ?
48
53
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 ( )
52
57
public private( set) var websocket = false
53
58
public private( set) var ws : WebSocket ?
54
59
@@ -59,11 +64,9 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
59
64
private typealias Probe = ( msg: String , type: SocketEnginePacketType , data: [ NSData ] )
60
65
private typealias ProbeWaitQueue = [ Probe ]
61
66
62
- private let allowedCharacterSet = NSCharacterSet ( charactersInString: " !*'();:@&=+$,/?%#[] \" {} " ) . invertedSet
63
67
private let logType = " SocketEngine "
64
- private let url : String
68
+ private let url : NSURL
65
69
66
- private var connectParams : [ String : AnyObject ] ?
67
70
private var pingInterval : Double ?
68
71
private var pingTimeout = 0.0 {
69
72
didSet {
@@ -76,13 +79,15 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
76
79
private var secure = false
77
80
private var selfSigned = false
78
81
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 > ) {
81
84
self . client = client
82
85
self . url = url
83
-
86
+
84
87
for option in options {
85
88
switch option {
89
+ case let . ConnectParams( params) :
90
+ connectParams = params
86
91
case let . SessionDelegate( delegate) :
87
92
sessionDelegate = delegate
88
93
case let . ForcePolling( force) :
@@ -105,11 +110,26 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
105
110
continue
106
111
}
107
112
}
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 ( ) ?? [ ] )
108
121
}
109
122
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 ( ) ?? [ ] )
113
133
}
114
134
115
135
deinit {
@@ -131,7 +151,7 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
131
151
switch code {
132
152
case 0 : // Unknown transport
133
153
didError ( error)
134
- case 1 : // Unknown sid. clear and retry connect
154
+ case 1 : // Unknown sid.
135
155
didError ( error)
136
156
case 2 : // Bad handshake request
137
157
didError ( error)
@@ -188,49 +208,45 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
188
208
}
189
209
}
190
210
191
- private func createURLs( params : [ String : AnyObject ] ? ) -> ( String , String ) {
211
+ private func createURLs( ) -> ( NSURL , NSURL ) {
192
212
if client == nil {
193
- return ( " " , " " )
213
+ return ( NSURL ( ) , NSURL ( ) )
194
214
}
195
215
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 "
199
224
200
225
if secure {
201
- urlPolling = " https:// " + socketURL + " polling "
202
- urlWebSocket = " wss:// " + socketURL + " websocket "
226
+ urlPolling. scheme = " https "
227
+ urlWebSocket. scheme = " wss "
203
228
} else {
204
- urlPolling = " http:// " + socketURL + " polling "
205
- urlWebSocket = " ws:// " + socketURL + " websocket "
229
+ urlPolling. scheme = " http "
230
+ urlWebSocket. scheme = " ws "
206
231
}
207
232
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) "
224
236
}
225
237
}
226
238
227
- return ( urlPolling, urlWebSocket)
239
+ urlWebSocket. query = urlWebSocket. query! + queryString
240
+ urlPolling. query = urlPolling. query! + queryString
241
+
242
+ return ( urlPolling. URL!, urlWebSocket. URL!)
228
243
}
229
244
230
245
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!)
234
250
235
251
if cookies != nil {
236
252
let headers = NSHTTPCookie . requestHeaderFieldsWithCookies ( cookies!)
@@ -363,30 +379,26 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
363
379
}
364
380
}
365
381
366
- public func open( opts : [ String : AnyObject ] ? ) {
382
+ public func open( ) {
367
383
if connected {
368
384
DefaultSocketLogger . Logger. error ( " Engine tried opening while connected. This is probably a programming error. "
369
385
+ " Abandoning open attempt " , type: logType)
370
386
return
371
387
}
372
388
373
- connectParams = opts
374
-
375
389
DefaultSocketLogger . Logger. log ( " Starting engine " , type: logType)
376
390
DefaultSocketLogger . Logger. log ( " Handshaking " , type: logType)
377
391
378
392
resetEngine ( )
379
393
380
- ( urlPolling, urlWebSocket) = createURLs ( opts)
381
-
382
394
if forceWebsockets {
383
395
polling = false
384
396
websocket = true
385
397
createWebsocketAndConnect ( )
386
398
return
387
399
}
388
400
389
- let reqPolling = NSMutableURLRequest ( URL: NSURL ( string : urlPolling + " &b64=1 " ) ! )
401
+ let reqPolling = NSMutableURLRequest ( URL: urlPolling)
390
402
391
403
if cookies != nil {
392
404
let headers = NSHTTPCookie . requestHeaderFieldsWithCookies ( cookies!)
0 commit comments