You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
7.2 KiB

2 years ago
  1. //
  2. // SocketEngineSpec.swift
  3. // Socket.IO-Client-Swift
  4. //
  5. // Created by Erik Little on 10/7/15.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. import Foundation
  26. import Starscream
  27. /// Specifies a SocketEngine.
  28. public protocol SocketEngineSpec: class {
  29. // MARK: Properties
  30. /// The client for this engine.
  31. var client: SocketEngineClient? { get set }
  32. /// `true` if this engine is closed.
  33. var closed: Bool { get }
  34. /// If `true` the engine will attempt to use WebSocket compression.
  35. var compress: Bool { get }
  36. /// `true` if this engine is connected. Connected means that the initial poll connect has succeeded.
  37. var connected: Bool { get }
  38. /// The connect parameters sent during a connect.
  39. var connectParams: [String: Any]? { get set }
  40. /// An array of HTTPCookies that are sent during the connection.
  41. var cookies: [HTTPCookie]? { get }
  42. /// The queue that all engine actions take place on.
  43. var engineQueue: DispatchQueue { get }
  44. /// A dictionary of extra http headers that will be set during connection.
  45. var extraHeaders: [String: String]? { get set }
  46. /// When `true`, the engine is in the process of switching to WebSockets.
  47. var fastUpgrade: Bool { get }
  48. /// When `true`, the engine will only use HTTP long-polling as a transport.
  49. var forcePolling: Bool { get }
  50. /// When `true`, the engine will only use WebSockets as a transport.
  51. var forceWebsockets: Bool { get }
  52. /// If `true`, the engine is currently in HTTP long-polling mode.
  53. var polling: Bool { get }
  54. /// If `true`, the engine is currently seeing whether it can upgrade to WebSockets.
  55. var probing: Bool { get }
  56. /// The session id for this engine.
  57. var sid: String { get }
  58. /// The path to engine.io.
  59. var socketPath: String { get }
  60. /// The url for polling.
  61. var urlPolling: URL { get }
  62. /// The url for WebSockets.
  63. var urlWebSocket: URL { get }
  64. /// The version of engine.io being used. Default is three.
  65. var version: SocketIOVersion { get }
  66. /// If `true`, then the engine is currently in WebSockets mode.
  67. @available(*, deprecated, message: "No longer needed, if we're not polling, then we must be doing websockets")
  68. var websocket: Bool { get }
  69. /// The WebSocket for this engine.
  70. var ws: WebSocket? { get }
  71. // MARK: Initializers
  72. /// Creates a new engine.
  73. ///
  74. /// - parameter client: The client for this engine.
  75. /// - parameter url: The url for this engine.
  76. /// - parameter options: The options for this engine.
  77. init(client: SocketEngineClient, url: URL, options: [String: Any]?)
  78. // MARK: Methods
  79. /// Starts the connection to the server.
  80. func connect()
  81. /// Called when an error happens during execution. Causes a disconnection.
  82. func didError(reason: String)
  83. /// Disconnects from the server.
  84. ///
  85. /// - parameter reason: The reason for the disconnection. This is communicated up to the client.
  86. func disconnect(reason: String)
  87. /// Called to switch from HTTP long-polling to WebSockets. After calling this method the engine will be in
  88. /// WebSocket mode.
  89. ///
  90. /// **You shouldn't call this directly**
  91. func doFastUpgrade()
  92. /// Causes any packets that were waiting for POSTing to be sent through the WebSocket. This happens because when
  93. /// the engine is attempting to upgrade to WebSocket it does not do any POSTing.
  94. ///
  95. /// **You shouldn't call this directly**
  96. func flushWaitingForPostToWebSocket()
  97. /// Parses raw binary received from engine.io.
  98. ///
  99. /// - parameter data: The data to parse.
  100. func parseEngineData(_ data: Data)
  101. /// Parses a raw engine.io packet.
  102. ///
  103. /// - parameter message: The message to parse.
  104. func parseEngineMessage(_ message: String)
  105. /// Writes a message to engine.io, independent of transport.
  106. ///
  107. /// - parameter msg: The message to send.
  108. /// - parameter type: The type of this message.
  109. /// - parameter data: Any data that this message has.
  110. /// - parameter completion: Callback called on transport write completion.
  111. func write(_ msg: String, withType type: SocketEnginePacketType, withData data: [Data], completion: (() -> ())?)
  112. }
  113. extension SocketEngineSpec {
  114. var engineIOParam: String {
  115. switch version {
  116. case .two:
  117. return "&EIO=3"
  118. case .three:
  119. return "&EIO=4"
  120. }
  121. }
  122. var urlPollingWithSid: URL {
  123. var com = URLComponents(url: urlPolling, resolvingAgainstBaseURL: false)!
  124. com.percentEncodedQuery = com.percentEncodedQuery! + "&sid=\(sid.urlEncode()!)"
  125. if !com.percentEncodedQuery!.contains("EIO") {
  126. com.percentEncodedQuery = com.percentEncodedQuery! + engineIOParam
  127. }
  128. return com.url!
  129. }
  130. var urlWebSocketWithSid: URL {
  131. var com = URLComponents(url: urlWebSocket, resolvingAgainstBaseURL: false)!
  132. com.percentEncodedQuery = com.percentEncodedQuery! + (sid == "" ? "" : "&sid=\(sid.urlEncode()!)")
  133. if !com.percentEncodedQuery!.contains("EIO") {
  134. com.percentEncodedQuery = com.percentEncodedQuery! + engineIOParam
  135. }
  136. return com.url!
  137. }
  138. func addHeaders(to req: inout URLRequest, includingCookies additionalCookies: [HTTPCookie]? = nil) {
  139. var cookiesToAdd: [HTTPCookie] = cookies ?? []
  140. cookiesToAdd += additionalCookies ?? []
  141. if !cookiesToAdd.isEmpty {
  142. req.allHTTPHeaderFields = HTTPCookie.requestHeaderFields(with: cookiesToAdd)
  143. }
  144. if let extraHeaders = extraHeaders {
  145. for (headerName, value) in extraHeaders {
  146. req.setValue(value, forHTTPHeaderField: headerName)
  147. }
  148. }
  149. }
  150. func createBinaryDataForSend(using data: Data) -> Either<Data, String> {
  151. let prefixB64 = version.rawValue >= 3 ? "b" : "b4"
  152. if polling {
  153. return .right(prefixB64 + data.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: 0)))
  154. } else {
  155. return .left(version.rawValue >= 3 ? data : Data([0x4]) + data)
  156. }
  157. }
  158. /// Send an engine message (4)
  159. func send(_ msg: String, withData datas: [Data], completion: (() -> ())? = nil) {
  160. write(msg, withType: .message, withData: datas, completion: completion)
  161. }
  162. }