SocketIOClientOption.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. protocol ClientOption : CustomStringConvertible, Hashable {
  26. func getSocketIOOptionValue() -> AnyObject
  27. }
  28. public enum SocketIOClientOption : ClientOption {
  29. case ConnectParams([String: AnyObject])
  30. case Cookies([NSHTTPCookie])
  31. case DoubleEncodeUTF8(Bool)
  32. case ExtraHeaders([String: String])
  33. case ForceNew(Bool)
  34. case ForcePolling(Bool)
  35. case ForceWebsockets(Bool)
  36. case HandleQueue(dispatch_queue_t)
  37. case Log(Bool)
  38. case Logger(SocketLogger)
  39. case Nsp(String)
  40. case Path(String)
  41. case Reconnects(Bool)
  42. case ReconnectAttempts(Int)
  43. case ReconnectWait(Int)
  44. case Secure(Bool)
  45. case SelfSigned(Bool)
  46. case SessionDelegate(NSURLSessionDelegate)
  47. case VoipEnabled(Bool)
  48. public var description: String {
  49. let description: String
  50. switch self {
  51. case .ConnectParams:
  52. description = "connectParams"
  53. case .Cookies:
  54. description = "cookies"
  55. case .DoubleEncodeUTF8:
  56. description = "doubleEncodeUTF8"
  57. case .ExtraHeaders:
  58. description = "extraHeaders"
  59. case .ForceNew:
  60. description = "forceNew"
  61. case .ForcePolling:
  62. description = "forcePolling"
  63. case .ForceWebsockets:
  64. description = "forceWebsockets"
  65. case .HandleQueue:
  66. description = "handleQueue"
  67. case .Log:
  68. description = "log"
  69. case .Logger:
  70. description = "logger"
  71. case .Nsp:
  72. description = "nsp"
  73. case .Path:
  74. description = "path"
  75. case .Reconnects:
  76. description = "reconnects"
  77. case .ReconnectAttempts:
  78. description = "reconnectAttempts"
  79. case .ReconnectWait:
  80. description = "reconnectWait"
  81. case .Secure:
  82. description = "secure"
  83. case .SelfSigned:
  84. description = "selfSigned"
  85. case .SessionDelegate:
  86. description = "sessionDelegate"
  87. case .VoipEnabled:
  88. description = "voipEnabled"
  89. }
  90. return description
  91. }
  92. public var hashValue: Int {
  93. return description.hashValue
  94. }
  95. func getSocketIOOptionValue() -> AnyObject {
  96. let value: AnyObject
  97. switch self {
  98. case let .ConnectParams(params):
  99. value = params
  100. case let .Cookies(cookies):
  101. value = cookies
  102. case let .DoubleEncodeUTF8(encode):
  103. value = encode
  104. case let .ExtraHeaders(headers):
  105. value = headers
  106. case let .ForceNew(force):
  107. value = force
  108. case let .ForcePolling(force):
  109. value = force
  110. case let .ForceWebsockets(force):
  111. value = force
  112. case let .HandleQueue(queue):
  113. value = queue
  114. case let .Log(log):
  115. value = log
  116. case let .Logger(logger):
  117. value = logger
  118. case let .Nsp(nsp):
  119. value = nsp
  120. case let .Path(path):
  121. value = path
  122. case let .Reconnects(reconnects):
  123. value = reconnects
  124. case let .ReconnectAttempts(attempts):
  125. value = attempts
  126. case let .ReconnectWait(wait):
  127. value = wait
  128. case let .Secure(secure):
  129. value = secure
  130. case let .SelfSigned(signed):
  131. value = signed
  132. case let .SessionDelegate(delegate):
  133. value = delegate
  134. case let .VoipEnabled(enabled):
  135. value = enabled
  136. }
  137. return value
  138. }
  139. }
  140. public func ==(lhs: SocketIOClientOption, rhs: SocketIOClientOption) -> Bool {
  141. return lhs.description == rhs.description
  142. }
  143. extension Set where Element : ClientOption {
  144. mutating func insertIgnore(element: Element) {
  145. if !contains(element) {
  146. insert(element)
  147. }
  148. }
  149. }
  150. extension NSDictionary {
  151. private static func keyValueToSocketIOClientOption(key: String, value: AnyObject) -> SocketIOClientOption? {
  152. switch (key, value) {
  153. case let ("connectParams", params as [String: AnyObject]):
  154. return .ConnectParams(params)
  155. case let ("cookies", cookies as [NSHTTPCookie]):
  156. return .Cookies(cookies)
  157. case let ("doubleEncodeUTF8", encode as Bool):
  158. return .DoubleEncodeUTF8(encode)
  159. case let ("extraHeaders", headers as [String: String]):
  160. return .ExtraHeaders(headers)
  161. case let ("forceNew", force as Bool):
  162. return .ForceNew(force)
  163. case let ("forcePolling", force as Bool):
  164. return .ForcePolling(force)
  165. case let ("forceWebsockets", force as Bool):
  166. return .ForceWebsockets(force)
  167. case let ("handleQueue", queue as dispatch_queue_t):
  168. return .HandleQueue(queue)
  169. case let ("log", log as Bool):
  170. return .Log(log)
  171. case let ("logger", logger as SocketLogger):
  172. return .Logger(logger)
  173. case let ("nsp", nsp as String):
  174. return .Nsp(nsp)
  175. case let ("path", path as String):
  176. return .Path(path)
  177. case let ("reconnects", reconnects as Bool):
  178. return .Reconnects(reconnects)
  179. case let ("reconnectAttempts", attempts as Int):
  180. return .ReconnectAttempts(attempts)
  181. case let ("reconnectWait", wait as Int):
  182. return .ReconnectWait(wait)
  183. case let ("secure", secure as Bool):
  184. return .Secure(secure)
  185. case let ("selfSigned", selfSigned as Bool):
  186. return .SelfSigned(selfSigned)
  187. case let ("sessionDelegate", delegate as NSURLSessionDelegate):
  188. return .SessionDelegate(delegate)
  189. case let ("voipEnabled", enable as Bool):
  190. return .VoipEnabled(enable)
  191. default:
  192. return nil
  193. }
  194. }
  195. func toSocketOptionsSet() -> Set<SocketIOClientOption> {
  196. var options = Set<SocketIOClientOption>()
  197. for (rawKey, value) in self {
  198. if let key = rawKey as? String, opt = NSDictionary.keyValueToSocketIOClientOption(key, value: value) {
  199. options.insertIgnore(opt)
  200. }
  201. }
  202. return options
  203. }
  204. }