@@ -44,6 +44,9 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
4444 }
4545 }
4646
47+ /// A dictionary of extra http headers that will be set during connection.
48+ public var extraHeaders : [ String : String ] ?
49+
4750 /// A queue of engine.io messages waiting for POSTing
4851 ///
4952 /// **You should not touch this directly**
@@ -73,9 +76,6 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
7376 /// An array of HTTPCookies that are sent during the connection.
7477 public private( set) var cookies : [ HTTPCookie ] ?
7578
76- /// A dictionary of extra http headers that will be set during connection.
77- public private( set) var extraHeaders : [ String : String ] ?
78-
7979 /// When `true`, the engine is in the process of switching to WebSockets.
8080 ///
8181 /// **Do not touch this directly**
@@ -112,6 +112,7 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
112112 public private( set) var urlWebSocket = URL ( string: " http://localhost/ " ) !
113113
114114 /// If `true`, then the engine is currently in WebSockets mode.
115+ @available ( * , deprecated, message: " No longer needed, if we're not polling, then we must be doing websockets " )
115116 public private( set) var websocket = false
116117
117118 /// The WebSocket for this engine.
@@ -233,7 +234,6 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
233234
234235 if forceWebsockets {
235236 polling = false
236- websocket = true
237237 createWebSocketAndConnect ( )
238238 return
239239 }
@@ -283,18 +283,7 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
283283 ws? . delegate = nil // TODO this seems a bit defensive, is this really needed?
284284 var req = URLRequest ( url: urlWebSocketWithSid)
285285
286- if cookies != nil {
287- let headers = HTTPCookie . requestHeaderFields ( with: cookies!)
288- for (headerName, value) in headers {
289- req. setValue ( value, forHTTPHeaderField: headerName)
290- }
291- }
292-
293- if extraHeaders != nil {
294- for (headerName, value) in extraHeaders! {
295- req. setValue ( value, forHTTPHeaderField: headerName)
296- }
297- }
286+ addHeaders ( to: & req)
298287
299288 ws = WebSocket ( request: req)
300289 ws? . callbackQueue = engineQueue
@@ -323,19 +312,15 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
323312 }
324313
325314 private func _disconnect( reason: String ) {
326- guard connected else { return closeOutEngine ( reason: reason) }
315+ guard connected && !closed else { return closeOutEngine ( reason: reason) }
327316
328317 DefaultSocketLogger . Logger. log ( " Engine is being closed. " , type: SocketEngine . logType)
329318
330- if closed {
331- return closeOutEngine ( reason: reason)
332- }
333-
334- if websocket {
319+ if polling {
320+ disconnectPolling ( reason: reason)
321+ } else {
335322 sendWebSocketMessage ( " " , withType: . close, withData: [ ] )
336323 closeOutEngine ( reason: reason)
337- } else {
338- disconnectPolling ( reason: reason)
339324 }
340325 }
341326
@@ -358,8 +343,9 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
358343 " we'll probably disconnect soon. You should report this. " , type: SocketEngine . logType)
359344 }
360345
346+ DefaultSocketLogger . Logger. log ( " Switching to WebSockets " , type: SocketEngine . logType)
347+
361348 sendWebSocketMessage ( " " , withType: . upgrade, withData: [ ] )
362- websocket = true
363349 polling = false
364350 fastUpgrade = false
365351 probing = false
@@ -454,6 +440,9 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
454440
455441 // We should upgrade
456442 if message == " 3probe " {
443+ DefaultSocketLogger . Logger. log ( " Received probe response, should upgrade to WebSockets " ,
444+ type: SocketEngine . logType)
445+
457446 upgradeTransport ( )
458447 }
459448
@@ -520,7 +509,6 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
520509 sid = " "
521510 waitingForPoll = false
522511 waitingForPost = false
523- websocket = false
524512 }
525513
526514 private func sendPing( ) {
@@ -603,17 +591,20 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
603591 public func write( _ msg: String , withType type: SocketEnginePacketType , withData data: [ Data ] ) {
604592 engineQueue. async {
605593 guard self . connected else { return }
594+ guard !self . probing else {
595+ self . probeWait. append ( ( msg, type, data) )
606596
607- if self . websocket {
608- DefaultSocketLogger . Logger. log ( " Writing ws: \( msg) has data: \( data. count != 0 ) " ,
609- type: SocketEngine . logType)
610- self . sendWebSocketMessage ( msg, withType: type, withData: data)
611- } else if !self . probing {
597+ return
598+ }
599+
600+ if self . polling {
612601 DefaultSocketLogger . Logger. log ( " Writing poll: \( msg) has data: \( data. count != 0 ) " ,
613602 type: SocketEngine . logType)
614603 self . sendPollMessage ( msg, withType: type, withData: data)
615604 } else {
616- self . probeWait. append ( ( msg, type, data) )
605+ DefaultSocketLogger . Logger. log ( " Writing ws: \( msg) has data: \( data. count != 0 ) " ,
606+ type: SocketEngine . logType)
607+ self . sendWebSocketMessage ( msg, withType: type, withData: data)
617608 }
618609 }
619610 }
@@ -642,14 +633,14 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
642633 return
643634 }
644635
645- guard websocket else {
636+ guard !polling else {
646637 flushProbeWait ( )
647638
648639 return
649640 }
650641
651642 connected = false
652- websocket = false
643+ polling = true
653644
654645 if let reason = error? . localizedDescription {
655646 didError ( reason: reason)
0 commit comments