SocketIOClientOption.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // SocketIOClientOption .swift
  3. // Socket.IO-Client-Swift
  4. //
  5. // Created by Erik Little on 10/17/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. import Foundation
  25. import Starscream
  26. protocol ClientOption : CustomStringConvertible, Equatable {
  27. func getSocketIOOptionValue() -> Any
  28. }
  29. /// The options for a client.
  30. public enum SocketIOClientOption : ClientOption {
  31. /// If given, the WebSocket transport will attempt to use compression.
  32. case compress
  33. /// A dictionary of GET parameters that will be included in the connect url.
  34. case connectParams([String: Any])
  35. /// An array of cookies that will be sent during the initial connection.
  36. case cookies([HTTPCookie])
  37. /// Any extra HTTP headers that should be sent during the initial connection.
  38. case extraHeaders([String: String])
  39. /// If passed `true`, will cause the client to always create a new engine. Useful for debugging,
  40. /// or when you want to be sure no state from previous engines is being carried over.
  41. case forceNew(Bool)
  42. /// If passed `true`, the only transport that will be used will be HTTP long-polling.
  43. case forcePolling(Bool)
  44. /// If passed `true`, the only transport that will be used will be WebSockets.
  45. case forceWebsockets(Bool)
  46. /// If passed `true`, the WebSocket stream will be configured with the enableSOCKSProxy `true`.
  47. case enableSOCKSProxy(Bool)
  48. /// The queue that all interaction with the client should occur on. This is the queue that event handlers are
  49. /// called on.
  50. ///
  51. /// **This should be a serial queue! Concurrent queues are not supported and might cause crashes and races**.
  52. case handleQueue(DispatchQueue)
  53. /// If passed `true`, the client will log debug information. This should be turned off in production code.
  54. case log(Bool)
  55. /// Used to pass in a custom logger.
  56. case logger(SocketLogger)
  57. /// A custom path to socket.io. Only use this if the socket.io server is configured to look for this path.
  58. case path(String)
  59. /// If passed `false`, the client will not reconnect when it loses connection. Useful if you want full control
  60. /// over when reconnects happen.
  61. case reconnects(Bool)
  62. /// The number of times to try and reconnect before giving up. Pass `-1` to [never give up](https://www.youtube.com/watch?v=dQw4w9WgXcQ).
  63. case reconnectAttempts(Int)
  64. /// The minimum number of seconds to wait before reconnect attempts.
  65. case reconnectWait(Int)
  66. /// The maximum number of seconds to wait before reconnect attempts.
  67. case reconnectWaitMax(Int)
  68. /// The randomization factor for calculating reconnect jitter.
  69. case randomizationFactor(Double)
  70. /// Set `true` if your server is using secure transports.
  71. case secure(Bool)
  72. /// Allows you to set which certs are valid. Useful for SSL pinning.
  73. case security(SSLSecurity)
  74. /// If you're using a self-signed set. Only use for development.
  75. case selfSigned(Bool)
  76. /// Sets an NSURLSessionDelegate for the underlying engine. Useful if you need to handle self-signed certs.
  77. case sessionDelegate(URLSessionDelegate)
  78. // MARK: Properties
  79. /// The description of this option.
  80. public var description: String {
  81. let description: String
  82. switch self {
  83. case .compress:
  84. description = "compress"
  85. case .connectParams:
  86. description = "connectParams"
  87. case .cookies:
  88. description = "cookies"
  89. case .extraHeaders:
  90. description = "extraHeaders"
  91. case .forceNew:
  92. description = "forceNew"
  93. case .forcePolling:
  94. description = "forcePolling"
  95. case .forceWebsockets:
  96. description = "forceWebsockets"
  97. case .handleQueue:
  98. description = "handleQueue"
  99. case .log:
  100. description = "log"
  101. case .logger:
  102. description = "logger"
  103. case .path:
  104. description = "path"
  105. case .reconnects:
  106. description = "reconnects"
  107. case .reconnectAttempts:
  108. description = "reconnectAttempts"
  109. case .reconnectWait:
  110. description = "reconnectWait"
  111. case .reconnectWaitMax:
  112. description = "reconnectWaitMax"
  113. case .randomizationFactor:
  114. description = "randomizationFactor"
  115. case .secure:
  116. description = "secure"
  117. case .selfSigned:
  118. description = "selfSigned"
  119. case .security:
  120. description = "security"
  121. case .sessionDelegate:
  122. description = "sessionDelegate"
  123. case .enableSOCKSProxy:
  124. description = "enableSOCKSProxy"
  125. }
  126. return description
  127. }
  128. func getSocketIOOptionValue() -> Any {
  129. let value: Any
  130. switch self {
  131. case .compress:
  132. value = true
  133. case let .connectParams(params):
  134. value = params
  135. case let .cookies(cookies):
  136. value = cookies
  137. case let .extraHeaders(headers):
  138. value = headers
  139. case let .forceNew(force):
  140. value = force
  141. case let .forcePolling(force):
  142. value = force
  143. case let .forceWebsockets(force):
  144. value = force
  145. case let .handleQueue(queue):
  146. value = queue
  147. case let .log(log):
  148. value = log
  149. case let .logger(logger):
  150. value = logger
  151. case let .path(path):
  152. value = path
  153. case let .reconnects(reconnects):
  154. value = reconnects
  155. case let .reconnectAttempts(attempts):
  156. value = attempts
  157. case let .reconnectWait(wait):
  158. value = wait
  159. case let .reconnectWaitMax(wait):
  160. value = wait
  161. case let .randomizationFactor(factor):
  162. value = factor
  163. case let .secure(secure):
  164. value = secure
  165. case let .security(security):
  166. value = security
  167. case let .selfSigned(signed):
  168. value = signed
  169. case let .sessionDelegate(delegate):
  170. value = delegate
  171. case let .enableSOCKSProxy(enable):
  172. value = enable
  173. }
  174. return value
  175. }
  176. // MARK: Operators
  177. /// Compares whether two options are the same.
  178. ///
  179. /// - parameter lhs: Left operand to compare.
  180. /// - parameter rhs: Right operand to compare.
  181. /// - returns: `true` if the two are the same option.
  182. public static func ==(lhs: SocketIOClientOption, rhs: SocketIOClientOption) -> Bool {
  183. return lhs.description == rhs.description
  184. }
  185. }