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.

128 lines
3.3 KiB

2 years ago
  1. [![Build Status](https://travis-ci.org/socketio/socket.io-client-swift.svg?branch=master)](https://travis-ci.org/socketio/socket.io-client-swift)
  2. # Socket.IO-Client-Swift
  3. Socket.IO-client for iOS/OS X.
  4. ## Example
  5. ```swift
  6. import SocketIO
  7. let manager = SocketManager(socketURL: URL(string: "http://localhost:8080")!, config: [.log(true), .compress])
  8. let socket = manager.defaultSocket
  9. socket.on(clientEvent: .connect) {data, ack in
  10. print("socket connected")
  11. }
  12. socket.on("currentAmount") {data, ack in
  13. guard let cur = data[0] as? Double else { return }
  14. socket.emitWithAck("canUpdate", cur).timingOut(after: 0) {data in
  15. if data.first as? String ?? "passed" == SocketAckValue.noAck {
  16. // Handle ack timeout
  17. }
  18. socket.emit("update", ["amount": cur + 2.50])
  19. }
  20. ack.with("Got your currentAmount", "dude")
  21. }
  22. socket.connect()
  23. ```
  24. ## Features
  25. - Supports socket.io 2.0+/3.0+.
  26. - Supports Binary
  27. - Supports Polling and WebSockets
  28. - Supports TLS/SSL
  29. ## FAQS
  30. Checkout the [FAQs](https://nuclearace.github.io/Socket.IO-Client-Swift/faq.html) for commonly asked questions.
  31. Checkout the [12to13](https://nuclearace.github.io/Socket.IO-Client-Swift/12to13.html) guide for migrating to v13+ from v12 below.
  32. Checkout the [15to16](https://nuclearace.github.io/Socket.IO-Client-Swift/15to16.html) guide for migrating to v16+ from v15.
  33. ## Installation
  34. Requires Swift 4/5 and Xcode 10.x
  35. ### Swift Package Manager
  36. Add the project as a dependency to your Package.swift:
  37. ```swift
  38. // swift-tools-version:4.2
  39. import PackageDescription
  40. let package = Package(
  41. name: "socket.io-test",
  42. products: [
  43. .executable(name: "socket.io-test", targets: ["YourTargetName"])
  44. ],
  45. dependencies: [
  46. .package(url: "https://github.com/socketio/socket.io-client-swift", .upToNextMinor(from: "15.0.0"))
  47. ],
  48. targets: [
  49. .target(name: "YourTargetName", dependencies: ["SocketIO"], path: "./Path/To/Your/Sources")
  50. ]
  51. )
  52. ```
  53. Then import `import SocketIO`.
  54. ### Carthage
  55. Add this line to your `Cartfile`:
  56. ```
  57. github "socketio/socket.io-client-swift" ~> 15.2.0
  58. ```
  59. Run `carthage update --platform ios,macosx`.
  60. Add the `Starscream` and `SocketIO` frameworks to your projects and follow the usual Carthage process.
  61. ### CocoaPods 1.0.0 or later
  62. Create `Podfile` and add `pod 'Socket.IO-Client-Swift'`:
  63. ```ruby
  64. use_frameworks!
  65. target 'YourApp' do
  66. pod 'Socket.IO-Client-Swift', '~> 15.2.0'
  67. end
  68. ```
  69. Install pods:
  70. ```
  71. $ pod install
  72. ```
  73. Import the module:
  74. Swift:
  75. ```swift
  76. import SocketIO
  77. ```
  78. Objective-C:
  79. ```Objective-C
  80. @import SocketIO;
  81. ```
  82. # [Docs](https://nuclearace.github.io/Socket.IO-Client-Swift/index.html)
  83. - [Client](https://nuclearace.github.io/Socket.IO-Client-Swift/Classes/SocketIOClient.html)
  84. - [Manager](https://nuclearace.github.io/Socket.IO-Client-Swift/Classes/SocketManager.html)
  85. - [Engine](https://nuclearace.github.io/Socket.IO-Client-Swift/Classes/SocketEngine.html)
  86. - [Options](https://nuclearace.github.io/Socket.IO-Client-Swift/Enums/SocketIOClientOption.html)
  87. ## Detailed Example
  88. A more detailed example can be found [here](https://github.com/nuclearace/socket.io-client-swift-example)
  89. An example using the Swift Package Manager can be found [here](https://github.com/nuclearace/socket.io-client-swift-spm-example)
  90. ## License
  91. MIT