Skip to content

Commit 1ff8e11

Browse files
committed
Document SocketIOClientSwift
add docs Start documenting engine More engine documentation Document engine document SocketEngineClient Document SocketEnginePollable and SocketEnginePacketType Document SocketEngineWebsocket Document SocketIOClient Document SocketIOClientStatus Document SocketLogger Document some typealiases Document SocketIOClientOption Document SocketIOClientConfiguration
1 parent a8ee018 commit 1ff8e11

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+17706
-114
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,5 @@ DerivedData
4848
Socket.IO-Test-Server/node_modules/*
4949

5050
.idea/
51+
docs/docsets/
52+
docs/undocumented.json

Source/SocketAckEmitter.swift

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public final class SocketAckEmitter : NSObject {
3232
let socket: SocketIOClient
3333
let ackNum: Int
3434

35+
// MARK: Properties
36+
3537
/// If true, this handler is expecting to be acked. Call `with(_: SocketData...)` to ack.
3638
public var expected: Bool {
3739
return ackNum != -1
@@ -67,6 +69,7 @@ public final class SocketAckEmitter : NSObject {
6769
/// A class that represents an emit that will request an ack that has not yet been sent.
6870
/// Call `timingOut(after:callback:)` to complete the emit
6971
/// Example:
72+
///
7073
/// ```swift
7174
/// socket.emitWithAck("myEvent").timingOut(after: 1) {data in
7275
/// ...

Source/SocketClientManager.swift

+33-12
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ import Foundation
2626

2727
/**
2828
Experimental socket manager.
29-
29+
3030
API subject to change.
31-
31+
3232
Can be used to persist sockets across ViewControllers.
33-
33+
3434
Sockets are strongly stored, so be sure to remove them once they are no
3535
longer needed.
36-
36+
3737
Example usage:
3838
```
3939
let manager = SocketClientManager.sharedManager
@@ -44,38 +44,59 @@ import Foundation
4444
```
4545
*/
4646
open class SocketClientManager : NSObject {
47+
// MARK: Properties.
48+
49+
/// The shared manager.
4750
open static let sharedManager = SocketClientManager()
48-
51+
4952
private var sockets = [String: SocketIOClient]()
50-
53+
54+
/// Gets a socket by its name.
55+
///
56+
/// - returns: The socket, if one had the given name.
5157
open subscript(string: String) -> SocketIOClient? {
5258
get {
5359
return sockets[string]
5460
}
55-
61+
5662
set(socket) {
5763
sockets[string] = socket
5864
}
5965
}
60-
66+
67+
// MARK: Methods.
68+
69+
/// Adds a socket.
70+
///
71+
/// - parameter socket: The socket to add.
72+
/// - parameter labeledAs: The label for this socket.
6173
open func addSocket(_ socket: SocketIOClient, labeledAs label: String) {
6274
sockets[label] = socket
6375
}
64-
76+
77+
/// Removes a socket by a given name.
78+
///
79+
/// - parameter withLabel: The label of the socket to remove.
80+
/// - returns: The socket for the given label, if one was present.
6581
open func removeSocket(withLabel label: String) -> SocketIOClient? {
6682
return sockets.removeValue(forKey: label)
6783
}
6884

85+
/// Removes a socket.
86+
///
87+
/// - parameter socket: The socket to remove.
88+
/// - returns: The socket if it was in the manager.
6989
open func removeSocket(_ socket: SocketIOClient) -> SocketIOClient? {
7090
var returnSocket: SocketIOClient?
71-
91+
7292
for (label, dictSocket) in sockets where dictSocket === socket {
7393
returnSocket = sockets.removeValue(forKey: label)
7494
}
75-
95+
7696
return returnSocket
7797
}
78-
98+
99+
/// Removes all the sockets in the manager.
79100
open func removeSockets() {
80101
sockets.removeAll()
81102
}

Source/SocketEngine.swift

+106-6
Original file line numberDiff line numberDiff line change
@@ -25,38 +25,96 @@
2525
import Dispatch
2626
import Foundation
2727

28+
/// The class that handles the engine.io protocol and transports.
29+
/// See `SocketEnginePollable` and `SocketEngineWebsocket` for transport specific methods.
2830
public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePollable, SocketEngineWebsocket {
29-
public let engineQueue = DispatchQueue(label: "com.socketio.engineHandleQueue", attributes: [])
31+
// MARK: Properties
3032

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.
3137
public var connectParams: [String: Any]? {
3238
didSet {
3339
(urlPolling, urlWebSocket) = createURLs()
3440
}
3541
}
3642

43+
/// A queue of engine.io messages waiting for POSTing
44+
///
45+
/// **You should not touch this directly**
3746
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**
3852
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**
3958
public var waitingForPost = false
4059

60+
/// `true` if this engine is closed.
4161
public private(set) var closed = false
62+
63+
/// `true` if this engine is connected. Connected means that the initial poll connect has succeeded.
4264
public private(set) var connected = false
65+
66+
/// An array of HTTPCookies that are sent during the connection.
4367
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.
4471
public private(set) var doubleEncodeUTF8 = true
72+
73+
/// A dictionary of extra http headers that will be set during connection.
4574
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**
4679
public private(set) var fastUpgrade = false
80+
81+
/// When `true`, the engine will only use HTTP long-polling as a transport.
4782
public private(set) var forcePolling = false
83+
84+
/// When `true`, the engine will only use WebSockets as a transport.
4885
public private(set) var forceWebsockets = false
86+
87+
/// `true` If engine's session has been invalidated.
4988
public private(set) var invalidated = false
89+
90+
/// If `true`, the engine is currently in HTTP long-polling mode.
5091
public private(set) var polling = true
92+
93+
/// If `true`, the engine is currently seeing whether it can upgrade to WebSockets.
5194
public private(set) var probing = false
95+
96+
/// The URLSession that will be used for polling.
5297
public private(set) var session: URLSession?
98+
99+
/// The session id for this engine.
53100
public private(set) var sid = ""
101+
102+
/// The path to engine.io.
54103
public private(set) var socketPath = "/engine.io/"
104+
105+
/// The url for polling.
55106
public private(set) var urlPolling = URL(string: "http://localhost/")!
107+
108+
/// The url for WebSockets.
56109
public private(set) var urlWebSocket = URL(string: "http://localhost/")!
110+
111+
/// If `true`, then the engine is currently in WebSockets mode.
57112
public private(set) var websocket = false
113+
114+
/// The WebSocket for this engine.
58115
public private(set) var ws: WebSocket?
59116

117+
/// The client for this engine.
60118
public weak var client: SocketEngineClient?
61119

62120
private weak var sessionDelegate: URLSessionDelegate?
@@ -79,6 +137,13 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
79137
private var selfSigned = false
80138
private var voipEnabled = false
81139

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.
82147
public init(client: SocketEngineClient, url: URL, config: SocketIOClientConfiguration) {
83148
self.client = client
84149
self.url = url
@@ -124,6 +189,11 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
124189
(urlPolling, urlWebSocket) = createURLs()
125190
}
126191

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.
127197
public convenience init(client: SocketEngineClient, url: URL, options: NSDictionary?) {
128198
self.init(client: client, url: url, config: options?.toSocketConfiguration() ?? [])
129199
}
@@ -134,6 +204,8 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
134204
stopPolling()
135205
}
136206

207+
// MARK: Methods
208+
137209
private func checkAndHandleEngineError(_ msg: String) {
138210
do {
139211
let dict = try msg.toNSDictionary()
@@ -171,7 +243,7 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
171243
client?.engineDidClose(reason: reason)
172244
}
173245

174-
/// Starts the connection to the server
246+
/// Starts the connection to the server.
175247
public func connect() {
176248
engineQueue.async {
177249
self._connect()
@@ -273,12 +345,16 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
273345
ws?.connect()
274346
}
275347

348+
/// Called when an error happens during execution. Causes a disconnection.
276349
public func didError(reason: String) {
277350
DefaultSocketLogger.Logger.error("%@", type: logType, args: reason)
278351
client?.engineDidError(reason: reason)
279352
disconnect(reason: reason)
280353
}
281354

355+
/// Disconnects from the server.
356+
///
357+
/// - parameter reason: The reason for the disconnection. This is communicated up to the client.
282358
public func disconnect(reason: String) {
283359
engineQueue.async {
284360
self._disconnect(reason: reason)
@@ -311,6 +387,10 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
311387
closeOutEngine(reason: reason)
312388
}
313389

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**
314394
public func doFastUpgrade() {
315395
if waitingForPoll {
316396
DefaultSocketLogger.Logger.error("Outstanding poll when switched to WebSockets," +
@@ -339,8 +419,10 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
339419
}
340420
}
341421

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**
344426
public func flushWaitingForPostToWebSocket() {
345427
guard let ws = self.ws else { return }
346428

@@ -415,12 +497,20 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
415497
}
416498
}
417499

500+
/// Parses raw binary received from engine.io.
501+
///
502+
/// - parameter data: The data to parse.
418503
public func parseEngineData(_ data: Data) {
419504
DefaultSocketLogger.Logger.log("Got binary data: %@", type: "SocketEngine", args: data)
420505

421506
client?.parseEngineBinaryData(data.subdata(in: 1..<data.endIndex))
422507
}
423508

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.
424514
public func parseEngineMessage(_ message: String, fromPolling: Bool) {
425515
DefaultSocketLogger.Logger.log("Got message: %@", type: logType, args: message)
426516

@@ -506,7 +596,11 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
506596
}
507597
}
508598

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.
510604
public func write(_ msg: String, withType type: SocketEnginePacketType, withData data: [Data]) {
511605
engineQueue.async {
512606
guard self.connected else { return }
@@ -525,7 +619,9 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
525619
}
526620
}
527621

528-
// Delegate methods
622+
// MARK: Starscream delegate conformance
623+
624+
/// Delegate method for connection.
529625
public func websocketDidConnect(socket: WebSocket) {
530626
if !forceWebsockets {
531627
probing = true
@@ -537,6 +633,7 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
537633
}
538634
}
539635

636+
/// Delegate method for disconnection.
540637
public func websocketDidDisconnect(socket: WebSocket, error: NSError?) {
541638
probing = false
542639

@@ -562,6 +659,9 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
562659
}
563660

564661
extension SocketEngine {
662+
// MARK: URLSessionDelegate methods
663+
664+
/// Delegate called when the session becomes invalid.
565665
public func URLSession(session: URLSession, didBecomeInvalidWithError error: NSError?) {
566666
DefaultSocketLogger.Logger.error("Engine URLSession became invalid", type: "SocketEngine")
567667

Source/SocketEngineClient.swift

+23-1
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,32 @@
2525

2626
import Foundation
2727

28-
@objc public protocol SocketEngineClient {
28+
/// Declares that a type will be a delegate to an engine.
29+
@objc public protocol SocketEngineClient {
30+
// MARK: Methods
31+
32+
/// Called when the engine errors.
33+
///
34+
/// - parameter reason: The reason the engine errored.
2935
func engineDidError(reason: String)
36+
37+
/// Called when the engine closes.
38+
///
39+
/// - parameter reason: The reason that the engine closed.
3040
func engineDidClose(reason: String)
41+
42+
/// Called when the engine opens.
43+
///
44+
/// - parameter reason: The reason the engine opened.
3145
func engineDidOpen(reason: String)
46+
47+
/// Called when the engine has a message that must be parsed.
48+
///
49+
/// - parameter msg: The message that needs parsing.
3250
func parseEngineMessage(_ msg: String)
51+
52+
/// Called when the engine receives binary data.
53+
///
54+
/// - parameter data: The data the engine received.
3355
func parseEngineBinaryData(_ data: Data)
3456
}

0 commit comments

Comments
 (0)