SocketEngineWebsocket.swift 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //
  2. // SocketEngineWebsocket.swift
  3. // Socket.IO-Client-Swift
  4. //
  5. // Created by Erik Little on 1/15/16.
  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. /// Protocol that is used to implement socket.io WebSocket support
  28. public protocol SocketEngineWebsocket : SocketEngineSpec {
  29. // MARK: Methods
  30. /// Sends an engine.io message through the WebSocket transport.
  31. ///
  32. /// You shouldn't call this directly, instead call the `write` method on `SocketEngine`.
  33. ///
  34. /// - parameter message: The message to send.
  35. /// - parameter withType: The type of message to send.
  36. /// - parameter withData: The data associated with this message.
  37. /// - parameter completion: Callback called on transport write completion.
  38. func sendWebSocketMessage(_ str: String,
  39. withType type: SocketEnginePacketType,
  40. withData datas: [Data],
  41. completion: (() -> ())?)
  42. }
  43. // WebSocket methods
  44. extension SocketEngineWebsocket {
  45. func probeWebSocket() {
  46. if ws?.isConnected ?? false {
  47. sendWebSocketMessage("probe", withType: .ping, withData: [], completion: nil)
  48. }
  49. }
  50. /// Sends an engine.io message through the WebSocket transport.
  51. ///
  52. /// You shouldn't call this directly, instead call the `write` method on `SocketEngine`.
  53. ///
  54. /// - parameter message: The message to send.
  55. /// - parameter withType: The type of message to send.
  56. /// - parameter withData: The data associated with this message.
  57. /// - parameter completion: Callback called on transport write completion.
  58. public func sendWebSocketMessage(_ str: String,
  59. withType type: SocketEnginePacketType,
  60. withData data: [Data],
  61. completion: (() -> ())?
  62. ) {
  63. DefaultSocketLogger.Logger.log("Sending ws: \(str) as type: \(type.rawValue)", type: "SocketEngineWebSocket")
  64. ws?.write(string: "\(type.rawValue)\(str)")
  65. if data.count == 0 {
  66. completion?()
  67. }
  68. for item in data {
  69. if case let .left(bin) = createBinaryDataForSend(using: item) {
  70. ws?.write(data: bin, completion: completion)
  71. }
  72. }
  73. }
  74. }