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.

145 lines
5.6 KiB

5 years ago
  1. //
  2. // Created by Erik Little on 10/18/17.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. import Dispatch
  22. import Foundation
  23. // TODO Fix the types so that we aren't using concrete types
  24. ///
  25. /// A manager for a socket.io connection.
  26. ///
  27. /// A `SocketManagerSpec` is responsible for multiplexing multiple namespaces through a single `SocketEngineSpec`.
  28. ///
  29. /// Example with `SocketManager`:
  30. ///
  31. /// ```swift
  32. /// let manager = SocketManager(socketURL: URL(string:"http://localhost:8080/")!)
  33. /// let defaultNamespaceSocket = manager.defaultSocket
  34. /// let swiftSocket = manager.socket(forNamespace: "/swift")
  35. ///
  36. /// // defaultNamespaceSocket and swiftSocket both share a single connection to the server
  37. /// ```
  38. ///
  39. /// Sockets created through the manager are retained by the manager. So at the very least, a single strong reference
  40. /// to the manager must be maintained to keep sockets alive.
  41. ///
  42. /// To disconnect a socket and remove it from the manager, either call `SocketIOClient.disconnect()` on the socket,
  43. /// or call one of the `disconnectSocket` methods on this class.
  44. ///
  45. @objc
  46. public protocol SocketManagerSpec : AnyObject, SocketEngineClient {
  47. // MARK: Properties
  48. /// Returns the socket associated with the default namespace ("/").
  49. var defaultSocket: SocketIOClient { get }
  50. /// The engine for this manager.
  51. var engine: SocketEngineSpec? { get set }
  52. /// If `true` then every time `connect` is called, a new engine will be created.
  53. var forceNew: Bool { get set }
  54. // TODO Per socket queues?
  55. /// The queue that all interaction with the client should occur on. This is the queue that event handlers are
  56. /// called on.
  57. var handleQueue: DispatchQueue { get set }
  58. /// The sockets in this manager indexed by namespace.
  59. var nsps: [String: SocketIOClient] { get set }
  60. /// If `true`, this manager will try and reconnect on any disconnects.
  61. var reconnects: Bool { get set }
  62. /// The minimum number of seconds to wait before attempting to reconnect.
  63. var reconnectWait: Int { get set }
  64. /// The maximum number of seconds to wait before attempting to reconnect.
  65. var reconnectWaitMax: Int { get set }
  66. /// The randomization factor for calculating reconnect jitter.
  67. var randomizationFactor: Double { get set }
  68. /// The URL of the socket.io server.
  69. var socketURL: URL { get }
  70. /// The status of this manager.
  71. var status: SocketIOStatus { get }
  72. // MARK: Methods
  73. /// Connects the underlying transport.
  74. func connect()
  75. /// Connects a socket through this manager's engine.
  76. ///
  77. /// - parameter socket: The socket who we should connect through this manager.
  78. func connectSocket(_ socket: SocketIOClient)
  79. /// Called when the manager has disconnected from socket.io.
  80. ///
  81. /// - parameter reason: The reason for the disconnection.
  82. func didDisconnect(reason: String)
  83. /// Disconnects the manager and all associated sockets.
  84. func disconnect()
  85. /// Disconnects the given socket.
  86. ///
  87. /// - parameter socket: The socket to disconnect.
  88. func disconnectSocket(_ socket: SocketIOClient)
  89. /// Disconnects the socket associated with `forNamespace`.
  90. ///
  91. /// - parameter nsp: The namespace to disconnect from.
  92. func disconnectSocket(forNamespace nsp: String)
  93. /// Sends an event to the server on all namespaces in this manager.
  94. ///
  95. /// - parameter event: The event to send.
  96. /// - parameter items: The data to send with this event.
  97. func emitAll(_ event: String, withItems items: [Any])
  98. /// Tries to reconnect to the server.
  99. ///
  100. /// This will cause a `disconnect` event to be emitted, as well as an `reconnectAttempt` event.
  101. func reconnect()
  102. /// Removes the socket from the manager's control.
  103. /// After calling this method the socket should no longer be considered usable.
  104. ///
  105. /// - parameter socket: The socket to remove.
  106. /// - returns: The socket removed, if it was owned by the manager.
  107. @discardableResult
  108. func removeSocket(_ socket: SocketIOClient) -> SocketIOClient?
  109. /// Returns a `SocketIOClient` for the given namespace. This socket shares a transport with the manager.
  110. ///
  111. /// Calling multiple times returns the same socket.
  112. ///
  113. /// Sockets created from this method are retained by the manager.
  114. /// Call one of the `disconnectSocket` methods on the implementing class to remove the socket from manager control.
  115. /// Or call `SocketIOClient.disconnect()` on the client.
  116. ///
  117. /// - parameter nsp: The namespace for the socket.
  118. /// - returns: A `SocketIOClient` for the given namespace.
  119. func socket(forNamespace nsp: String) -> SocketIOClient
  120. }