24
24
25
25
import Dispatch
26
26
import Foundation
27
+ #if !os(Linux)
27
28
import StarscreamSocketIO
29
+ #else
30
+ import WebSockets
31
+ #endif
28
32
29
33
/// The class that handles the engine.io protocol and transports.
30
34
/// See `SocketEnginePollable` and `SocketEngineWebsocket` for transport specific methods.
@@ -60,6 +64,9 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
60
64
/// **Do not touch this directly**
61
65
public var waitingForPost = false
62
66
67
+ /// The WebSocket for this engine.
68
+ public var ws : WebSocket ?
69
+
63
70
/// `true` if this engine is closed.
64
71
public private( set) var closed = false
65
72
@@ -95,6 +102,17 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
95
102
/// If `true`, the engine is currently seeing whether it can upgrade to WebSockets.
96
103
public private( set) var probing = false
97
104
105
+ /// Whether or not this engine uses secure transports
106
+ public private( set) var secure = false
107
+
108
+ #if !os(Linux)
109
+ /// A custom security validator for Starscream. Useful for SSL pinning.
110
+ public private( set) var security : SSLSecurity ?
111
+ #endif
112
+
113
+ /// Whether or not to allow self signed certificates.
114
+ public private( set) var selfSigned = false
115
+
98
116
/// The URLSession that will be used for polling.
99
117
public private( set) var session : URLSession ?
100
118
@@ -113,9 +131,6 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
113
131
/// If `true`, then the engine is currently in WebSockets mode.
114
132
public private( set) var websocket = false
115
133
116
- /// The WebSocket for this engine.
117
- public private( set) var ws : WebSocket ?
118
-
119
134
/// The client for this engine.
120
135
public weak var client : SocketEngineClient ?
121
136
@@ -133,9 +148,6 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
133
148
private var pongsMissed = 0
134
149
private var pongsMissedMax = 0
135
150
private var probeWait = ProbeWaitQueue ( )
136
- private var secure = false
137
- private var security : SSLSecurity ?
138
- private var selfSigned = false
139
151
140
152
// MARK: Initializers
141
153
@@ -308,32 +320,6 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
308
320
return ( urlPolling. url!, urlWebSocket. url!)
309
321
}
310
322
311
- private func createWebSocketAndConnect( ) {
312
- ws? . delegate = nil // TODO this seems a bit defensive, is this really needed?
313
- ws = WebSocket ( url: urlWebSocketWithSid)
314
-
315
- if cookies != nil {
316
- let headers = HTTPCookie . requestHeaderFields ( with: cookies!)
317
- for (key, value) in headers {
318
- ws? . headers [ key] = value
319
- }
320
- }
321
-
322
- if extraHeaders != nil {
323
- for (headerName, value) in extraHeaders! {
324
- ws? . headers [ headerName] = value
325
- }
326
- }
327
-
328
- ws? . callbackQueue = engineQueue
329
- ws? . enableCompression = compress
330
- ws? . delegate = self
331
- ws? . disableSSLCertValidation = selfSigned
332
- ws? . security = security
333
-
334
- ws? . connect ( )
335
- }
336
-
337
323
/// Called when an error happens during execution. Causes a disconnection.
338
324
public func didError( reason: String ) {
339
325
DefaultSocketLogger . Logger. error ( " %@ " , type: SocketEngine . logType, args: reason)
@@ -486,6 +472,44 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
486
472
}
487
473
}
488
474
475
+ /// Called when a successful WebSocket connection is made.
476
+ public func handleWSConnect( ) {
477
+ if !forceWebsockets {
478
+ probing = true
479
+ probeWebSocket ( )
480
+ } else {
481
+ connected = true
482
+ probing = false
483
+ polling = false
484
+ }
485
+ }
486
+
487
+ /// Called when the WebSocket disconnects.
488
+ public func handleWSDisconnect( error: NSError ? ) {
489
+ probing = false
490
+
491
+ if closed {
492
+ client? . engineDidClose ( reason: " Disconnect " )
493
+
494
+ return
495
+ }
496
+
497
+ guard websocket else {
498
+ flushProbeWait ( )
499
+
500
+ return
501
+ }
502
+
503
+ connected = false
504
+ websocket = false
505
+
506
+ if let reason = error? . localizedDescription {
507
+ didError ( reason: reason)
508
+ } else {
509
+ client? . engineDidClose ( reason: " Socket Disconnected " )
510
+ }
511
+ }
512
+
489
513
/// Parses raw binary received from engine.io.
490
514
///
491
515
/// - parameter data: The data to parse.
@@ -601,45 +625,19 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
601
625
}
602
626
}
603
627
628
+ #if !os(Linux)
604
629
// MARK: Starscream delegate conformance
605
630
606
631
/// Delegate method for connection.
607
632
public func websocketDidConnect( socket: WebSocket ) {
608
- if !forceWebsockets {
609
- probing = true
610
- probeWebSocket ( )
611
- } else {
612
- connected = true
613
- probing = false
614
- polling = false
615
- }
633
+ handleWSConnect ( )
616
634
}
617
635
618
636
/// Delegate method for disconnection.
619
637
public func websocketDidDisconnect( socket: WebSocket , error: NSError ? ) {
620
- probing = false
621
-
622
- if closed {
623
- client? . engineDidClose ( reason: " Disconnect " )
624
-
625
- return
626
- }
627
-
628
- guard websocket else {
629
- flushProbeWait ( )
630
-
631
- return
632
- }
633
-
634
- connected = false
635
- websocket = false
636
-
637
- if let reason = error? . localizedDescription {
638
- didError ( reason: reason)
639
- } else {
640
- client? . engineDidClose ( reason: " Socket Disconnected " )
641
- }
638
+ handleWSDisconnect ( error: error)
642
639
}
640
+ #endif
643
641
}
644
642
645
643
extension SocketEngine {
0 commit comments