25
25
import Dispatch
26
26
import Foundation
27
27
28
+ /// The class that handles the engine.io protocol and transports.
29
+ /// See `SocketEnginePollable` and `SocketEngineWebsocket` for transport specific methods.
28
30
public final class SocketEngine : NSObject , URLSessionDelegate , SocketEnginePollable , SocketEngineWebsocket {
29
- public let engineQueue = DispatchQueue ( label : " com.socketio.engineHandleQueue " , attributes : [ ] )
31
+ // MARK: Properties
30
32
33
+ /// The queue that all engine actions take place on.
34
+ public let engineQueue = DispatchQueue ( label: " com.socketio.engineHandleQueue " )
35
+
36
+ /// The connect parameters sent during a connect.
31
37
public var connectParams : [ String : Any ] ? {
32
38
didSet {
33
39
( urlPolling, urlWebSocket) = createURLs ( )
34
40
}
35
41
}
36
42
43
+ /// A queue of engine.io messages waiting for POSTing
44
+ ///
45
+ /// **You should not touch this directly**
37
46
public var postWait = [ String] ( )
47
+
48
+ /// `true` if there is an outstanding poll. Trying to poll before the first is done will cause socket.io to
49
+ /// disconnect us.
50
+ ///
51
+ /// **Do not touch this directly**
38
52
public var waitingForPoll = false
53
+
54
+ /// `true` if there is an outstanding post. Trying to post before the first is done will cause socket.io to
55
+ /// disconnect us.
56
+ ///
57
+ /// **Do not touch this directly**
39
58
public var waitingForPost = false
40
59
60
+ /// `true` if this engine is closed.
41
61
public private( set) var closed = false
62
+
63
+ /// `true` if this engine is connected. Connected means that the initial poll connect has succeeded.
42
64
public private( set) var connected = false
65
+
66
+ /// An array of HTTPCookies that are sent during the connection.
43
67
public private( set) var cookies : [ HTTPCookie ] ?
68
+
69
+ /// Set to `true` if using the node.js version of socket.io. The node.js version of socket.io
70
+ /// handles utf8 incorrectly.
44
71
public private( set) var doubleEncodeUTF8 = true
72
+
73
+ /// A dictionary of extra http headers that will be set during connection.
45
74
public private( set) var extraHeaders : [ String : String ] ?
75
+
76
+ /// When `true`, the engine is in the process of switching to WebSockets.
77
+ ///
78
+ /// **Do not touch this directly**
46
79
public private( set) var fastUpgrade = false
80
+
81
+ /// When `true`, the engine will only use HTTP long-polling as a transport.
47
82
public private( set) var forcePolling = false
83
+
84
+ /// When `true`, the engine will only use WebSockets as a transport.
48
85
public private( set) var forceWebsockets = false
86
+
87
+ /// `true` If engine's session has been invalidated.
49
88
public private( set) var invalidated = false
89
+
90
+ /// If `true`, the engine is currently in HTTP long-polling mode.
50
91
public private( set) var polling = true
92
+
93
+ /// If `true`, the engine is currently seeing whether it can upgrade to WebSockets.
51
94
public private( set) var probing = false
95
+
96
+ /// The URLSession that will be used for polling.
52
97
public private( set) var session : URLSession ?
98
+
99
+ /// The session id for this engine.
53
100
public private( set) var sid = " "
101
+
102
+ /// The path to engine.io.
54
103
public private( set) var socketPath = " /engine.io/ "
104
+
105
+ /// The url for polling.
55
106
public private( set) var urlPolling = URL ( string: " http://localhost/ " ) !
107
+
108
+ /// The url for WebSockets.
56
109
public private( set) var urlWebSocket = URL ( string: " http://localhost/ " ) !
110
+
111
+ /// If `true`, then the engine is currently in WebSockets mode.
57
112
public private( set) var websocket = false
113
+
114
+ /// The WebSocket for this engine.
58
115
public private( set) var ws : WebSocket ?
59
116
117
+ /// The client for this engine.
60
118
public weak var client : SocketEngineClient ?
61
119
62
120
private weak var sessionDelegate : URLSessionDelegate ?
@@ -79,6 +137,13 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
79
137
private var selfSigned = false
80
138
private var voipEnabled = false
81
139
140
+ // MARK: Initializers
141
+
142
+ /// Creates a new engine.
143
+ ///
144
+ /// - parameter client: The client for this engine.
145
+ /// - parameter url: The url for this engine.
146
+ /// - parameter config: An array of configuration options for this engine.
82
147
public init ( client: SocketEngineClient , url: URL , config: SocketIOClientConfiguration ) {
83
148
self . client = client
84
149
self . url = url
@@ -124,6 +189,11 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
124
189
( urlPolling, urlWebSocket) = createURLs ( )
125
190
}
126
191
192
+ /// Creates a new engine.
193
+ ///
194
+ /// - parameter client: The client for this engine.
195
+ /// - parameter url: The url for this engine.
196
+ /// - parameter options: The options for this engine.
127
197
public convenience init ( client: SocketEngineClient , url: URL , options: NSDictionary ? ) {
128
198
self . init ( client: client, url: url, config: options? . toSocketConfiguration ( ) ?? [ ] )
129
199
}
@@ -134,6 +204,8 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
134
204
stopPolling ( )
135
205
}
136
206
207
+ // MARK: Methods
208
+
137
209
private func checkAndHandleEngineError( _ msg: String ) {
138
210
do {
139
211
let dict = try msg. toNSDictionary ( )
@@ -171,7 +243,7 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
171
243
client? . engineDidClose ( reason: reason)
172
244
}
173
245
174
- /// Starts the connection to the server
246
+ /// Starts the connection to the server.
175
247
public func connect( ) {
176
248
engineQueue. async {
177
249
self . _connect ( )
@@ -273,12 +345,16 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
273
345
ws? . connect ( )
274
346
}
275
347
348
+ /// Called when an error happens during execution. Causes a disconnection.
276
349
public func didError( reason: String ) {
277
350
DefaultSocketLogger . Logger. error ( " %@ " , type: logType, args: reason)
278
351
client? . engineDidError ( reason: reason)
279
352
disconnect ( reason: reason)
280
353
}
281
354
355
+ /// Disconnects from the server.
356
+ ///
357
+ /// - parameter reason: The reason for the disconnection. This is communicated up to the client.
282
358
public func disconnect( reason: String ) {
283
359
engineQueue. async {
284
360
self . _disconnect ( reason: reason)
@@ -311,6 +387,10 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
311
387
closeOutEngine ( reason: reason)
312
388
}
313
389
390
+ /// Called to switch from HTTP long-polling to WebSockets. After calling this method the engine will be in
391
+ /// WebSocket mode.
392
+ ///
393
+ /// **You shouldn't call this directly**
314
394
public func doFastUpgrade( ) {
315
395
if waitingForPoll {
316
396
DefaultSocketLogger . Logger. error ( " Outstanding poll when switched to WebSockets, " +
@@ -339,8 +419,10 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
339
419
}
340
420
}
341
421
342
- // We had packets waiting for send when we upgraded
343
- // Send them raw
422
+ /// Causes any packets that were waiting for POSTing to be sent through the WebSocket. This happens because when
423
+ /// the engine is attempting to upgrade to WebSocket it does not do any POSTing.
424
+ ///
425
+ /// **You shouldn't call this directly**
344
426
public func flushWaitingForPostToWebSocket( ) {
345
427
guard let ws = self . ws else { return }
346
428
@@ -415,12 +497,20 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
415
497
}
416
498
}
417
499
500
+ /// Parses raw binary received from engine.io.
501
+ ///
502
+ /// - parameter data: The data to parse.
418
503
public func parseEngineData( _ data: Data ) {
419
504
DefaultSocketLogger . Logger. log ( " Got binary data: %@ " , type: " SocketEngine " , args: data)
420
505
421
506
client? . parseEngineBinaryData ( data. subdata ( in: 1 ..< data. endIndex) )
422
507
}
423
508
509
+ /// Parses a raw engine.io packet.
510
+ ///
511
+ /// - parameter message: The message to parse.
512
+ /// - parameter fromPolling: Whether this message is from long-polling.
513
+ /// If `true` we might have to fix utf8 encoding.
424
514
public func parseEngineMessage( _ message: String , fromPolling: Bool ) {
425
515
DefaultSocketLogger . Logger. log ( " Got message: %@ " , type: logType, args: message)
426
516
@@ -506,7 +596,11 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
506
596
}
507
597
}
508
598
509
- /// Write a message, independent of transport.
599
+ /// Writes a message to engine.io, independent of transport.
600
+ ///
601
+ /// - parameter msg: The message to send.
602
+ /// - parameter withType: The type of this message.
603
+ /// - parameter withData: Any data that this message has.
510
604
public func write( _ msg: String , withType type: SocketEnginePacketType , withData data: [ Data ] ) {
511
605
engineQueue. async {
512
606
guard self . connected else { return }
@@ -525,7 +619,9 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
525
619
}
526
620
}
527
621
528
- // Delegate methods
622
+ // MARK: Starscream delegate conformance
623
+
624
+ /// Delegate method for connection.
529
625
public func websocketDidConnect( socket: WebSocket ) {
530
626
if !forceWebsockets {
531
627
probing = true
@@ -537,6 +633,7 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
537
633
}
538
634
}
539
635
636
+ /// Delegate method for disconnection.
540
637
public func websocketDidDisconnect( socket: WebSocket , error: NSError ? ) {
541
638
probing = false
542
639
@@ -562,6 +659,9 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
562
659
}
563
660
564
661
extension SocketEngine {
662
+ // MARK: URLSessionDelegate methods
663
+
664
+ /// Delegate called when the session becomes invalid.
565
665
public func URLSession( session: URLSession , didBecomeInvalidWithError error: NSError ? ) {
566
666
DefaultSocketLogger . Logger. error ( " Engine URLSession became invalid " , type: " SocketEngine " )
567
667
0 commit comments