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.

163 lines
6.3 KiB

5 years ago
  1. //
  2. // SocketRawView.swift
  3. // Socket.IO-Client-Swift
  4. //
  5. // Created by Erik Little on 3/30/18.
  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. /// Class that gives a backwards compatible way to cause an emit not to recursively check for Data objects.
  26. ///
  27. /// Usage:
  28. ///
  29. /// ```swift
  30. /// socket.rawEmitView.emit("myEvent", myObject)
  31. /// ```
  32. public final class SocketRawView : NSObject {
  33. private unowned let socket: SocketIOClient
  34. init(socket: SocketIOClient) {
  35. self.socket = socket
  36. }
  37. /// Send an event to the server, with optional data items.
  38. ///
  39. /// If an error occurs trying to transform `items` into their socket representation, a `SocketClientEvent.error`
  40. /// will be emitted. The structure of the error data is `[eventName, items, theError]`
  41. ///
  42. /// - parameter event: The event to send.
  43. /// - parameter items: The items to send with this event. May be left out.
  44. public func emit(_ event: String, _ items: SocketData...) {
  45. do {
  46. try emit(event, with: items.map({ try $0.socketRepresentation() }))
  47. } catch {
  48. DefaultSocketLogger.Logger.error("Error creating socketRepresentation for emit: \(event), \(items)",
  49. type: "SocketIOClient")
  50. socket.handleClientEvent(.error, data: [event, items, error])
  51. }
  52. }
  53. /// Same as emit, but meant for Objective-C
  54. ///
  55. /// - parameter event: The event to send.
  56. /// - parameter items: The items to send with this event. Send an empty array to send no data.
  57. @objc
  58. public func emit(_ event: String, with items: [Any]) {
  59. socket.emit([event] + items, binary: false)
  60. }
  61. /// Sends a message to the server, requesting an ack.
  62. ///
  63. /// **NOTE**: It is up to the server send an ack back, just calling this method does not mean the server will ack.
  64. /// Check that your server's api will ack the event being sent.
  65. ///
  66. /// If an error occurs trying to transform `items` into their socket representation, a `SocketClientEvent.error`
  67. /// will be emitted. The structure of the error data is `[eventName, items, theError]`
  68. ///
  69. /// Example:
  70. ///
  71. /// ```swift
  72. /// socket.emitWithAck("myEvent", 1).timingOut(after: 1) {data in
  73. /// ...
  74. /// }
  75. /// ```
  76. ///
  77. /// - parameter event: The event to send.
  78. /// - parameter items: The items to send with this event. May be left out.
  79. /// - returns: An `OnAckCallback`. You must call the `timingOut(after:)` method before the event will be sent.
  80. public func emitWithAck(_ event: String, _ items: SocketData...) -> OnAckCallback {
  81. do {
  82. return emitWithAck(event, with: try items.map({ try $0.socketRepresentation() }))
  83. } catch {
  84. DefaultSocketLogger.Logger.error("Error creating socketRepresentation for emit: \(event), \(items)",
  85. type: "SocketIOClient")
  86. socket.handleClientEvent(.error, data: [event, items, error])
  87. return OnAckCallback(ackNumber: -1, items: [], socket: socket)
  88. }
  89. }
  90. /// Same as emitWithAck, but for Objective-C
  91. ///
  92. /// **NOTE**: It is up to the server send an ack back, just calling this method does not mean the server will ack.
  93. /// Check that your server's api will ack the event being sent.
  94. ///
  95. /// Example:
  96. ///
  97. /// ```swift
  98. /// socket.emitWithAck("myEvent", with: [1]).timingOut(after: 1) {data in
  99. /// ...
  100. /// }
  101. /// ```
  102. ///
  103. /// - parameter event: The event to send.
  104. /// - parameter items: The items to send with this event. Use `[]` to send nothing.
  105. /// - returns: An `OnAckCallback`. You must call the `timingOut(after:)` method before the event will be sent.
  106. @objc
  107. public func emitWithAck(_ event: String, with items: [Any]) -> OnAckCallback {
  108. return socket.createOnAck([event] + items, binary: false)
  109. }
  110. }
  111. /// Class that gives a backwards compatible way to cause an emit not to recursively check for Data objects.
  112. ///
  113. /// Usage:
  114. ///
  115. /// ```swift
  116. /// ack.rawEmitView.with(myObject)
  117. /// ```
  118. public final class SocketRawAckView : NSObject {
  119. private unowned let socket: SocketIOClient
  120. private let ackNum: Int
  121. init(socket: SocketIOClient, ackNum: Int) {
  122. self.socket = socket
  123. self.ackNum = ackNum
  124. }
  125. /// Call to ack receiving this event.
  126. ///
  127. /// If an error occurs trying to transform `items` into their socket representation, a `SocketClientEvent.error`
  128. /// will be emitted. The structure of the error data is `[ackNum, items, theError]`
  129. ///
  130. /// - parameter items: A variable number of items to send when acking.
  131. public func with(_ items: SocketData...) {
  132. guard ackNum != -1 else { return }
  133. do {
  134. socket.emit(try items.map({ try $0.socketRepresentation() }), ack: ackNum, binary: false, isAck: true)
  135. } catch {
  136. socket.handleClientEvent(.error, data: [ackNum, items, error])
  137. }
  138. }
  139. /// Call to ack receiving this event.
  140. ///
  141. /// - parameter items: An array of items to send when acking. Use `[]` to send nothing.
  142. @objc
  143. public func with(_ items: [Any]) {
  144. guard ackNum != -1 else { return }
  145. socket.emit(items, ack: ackNum, binary: false, isAck: true)
  146. }
  147. }