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.

87 lines
3.4 KiB

2 years ago
  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: Properties
  30. /// Whether or not the ws is connected
  31. var wsConnected: Bool { get }
  32. // MARK: Methods
  33. /// Sends an engine.io message through the WebSocket transport.
  34. ///
  35. /// You shouldn't call this directly, instead call the `write` method on `SocketEngine`.
  36. ///
  37. /// - parameter message: The message to send.
  38. /// - parameter withType: The type of message to send.
  39. /// - parameter withData: The data associated with this message.
  40. /// - parameter completion: Callback called on transport write completion.
  41. func sendWebSocketMessage(_ str: String,
  42. withType type: SocketEnginePacketType,
  43. withData datas: [Data],
  44. completion: (() -> ())?)
  45. }
  46. // WebSocket methods
  47. extension SocketEngineWebsocket {
  48. func probeWebSocket() {
  49. if wsConnected {
  50. sendWebSocketMessage("probe", withType: .ping, withData: [], completion: nil)
  51. }
  52. }
  53. /// Sends an engine.io message through the WebSocket transport.
  54. ///
  55. /// You shouldn't call this directly, instead call the `write` method on `SocketEngine`.
  56. ///
  57. /// - parameter message: The message to send.
  58. /// - parameter withType: The type of message to send.
  59. /// - parameter withData: The data associated with this message.
  60. /// - parameter completion: Callback called on transport write completion.
  61. public func sendWebSocketMessage(_ str: String,
  62. withType type: SocketEnginePacketType,
  63. withData data: [Data],
  64. completion: (() -> ())?
  65. ) {
  66. DefaultSocketLogger.Logger.log("Sending ws: \(str) as type: \(type.rawValue)", type: "SocketEngineWebSocket")
  67. ws?.write(string: "\(type.rawValue)\(str)")
  68. for item in data {
  69. if case let .left(bin) = createBinaryDataForSend(using: item) {
  70. ws?.write(data: bin, completion: completion)
  71. }
  72. }
  73. if data.count == 0 {
  74. completion?()
  75. }
  76. }
  77. }