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.

153 lines
4.0 KiB

5 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. socket.emit("update", ["amount": cur + 2.50])
  16. }
  17. ack.with("Got your currentAmount", "dude")
  18. }
  19. socket.connect()
  20. ```
  21. ## Objective-C Example
  22. ```objective-c
  23. @import SocketIO;
  24. NSURL* url = [[NSURL alloc] initWithString:@"http://localhost:8080"];
  25. SocketManager* manager = [[SocketManager alloc] initWithSocketURL:url config:@{@"log": @YES, @"compress": @YES}];
  26. SocketIOClient* socket = manager.defaultSocket;
  27. [socket on:@"connect" callback:^(NSArray* data, SocketAckEmitter* ack) {
  28. NSLog(@"socket connected");
  29. }];
  30. [socket on:@"currentAmount" callback:^(NSArray* data, SocketAckEmitter* ack) {
  31. double cur = [[data objectAtIndex:0] floatValue];
  32. [[socket emitWithAck:@"canUpdate" with:@[@(cur)]] timingOutAfter:0 callback:^(NSArray* data) {
  33. [socket emit:@"update" with:@[@{@"amount": @(cur + 2.50)}]];
  34. }];
  35. [ack with:@[@"Got your currentAmount, ", @"dude"]];
  36. }];
  37. [socket connect];
  38. ```
  39. ## Features
  40. - Supports socket.io 2.0+ (For socket.io 1.0 use v9.x)
  41. - Supports binary
  42. - Supports Polling and WebSockets
  43. - Supports TLS/SSL
  44. - Can be used from Objective-C
  45. ## FAQS
  46. Checkout the [FAQs](https://nuclearace.github.io/Socket.IO-Client-Swift/faq.html) for commonly asked questions.
  47. Checkout the [12to13](https://nuclearace.github.io/Socket.IO-Client-Swift/12to13.html) guide for migrating to v13+ from v12 below.
  48. ## Installation
  49. Requires Swift 4/5 and Xcode 10.x
  50. If you need Swift 2.3 use the [swift2.3 tag](https://github.com/socketio/socket.io-client-swift/releases/tag/swift2.3) (Pre-Swift 4 support is no longer maintained)
  51. If you need Swift 3.x use v11.1.3.
  52. ### Swift Package Manager
  53. Add the project as a dependency to your Package.swift:
  54. ```swift
  55. // swift-tools-version:4.2
  56. import PackageDescription
  57. let package = Package(
  58. name: "socket.io-test",
  59. products: [
  60. .executable(name: "socket.io-test", targets: ["YourTargetName"])
  61. ],
  62. dependencies: [
  63. .package(url: "https://github.com/socketio/socket.io-client-swift", .upToNextMinor(from: "15.0.0"))
  64. ],
  65. targets: [
  66. .target(name: "YourTargetName", dependencies: ["SocketIO"], path: "./Path/To/Your/Sources")
  67. ]
  68. )
  69. ```
  70. Then import `import SocketIO`.
  71. ### Carthage
  72. Add this line to your `Cartfile`:
  73. ```
  74. github "socketio/socket.io-client-swift" ~> 15.0.0
  75. ```
  76. Run `carthage update --platform ios,macosx`.
  77. Add the `Starscream` and `SocketIO` frameworks to your projects and follow the usual Carthage process.
  78. ### CocoaPods 1.0.0 or later
  79. Create `Podfile` and add `pod 'Socket.IO-Client-Swift'`:
  80. ```ruby
  81. use_frameworks!
  82. target 'YourApp' do
  83. pod 'Socket.IO-Client-Swift', '~> 15.0.0'
  84. end
  85. ```
  86. Install pods:
  87. ```
  88. $ pod install
  89. ```
  90. Import the module:
  91. Swift:
  92. ```swift
  93. import SocketIO
  94. ```
  95. Objective-C:
  96. ```Objective-C
  97. @import SocketIO;
  98. ```
  99. # [Docs](https://nuclearace.github.io/Socket.IO-Client-Swift/index.html)
  100. - [Client](https://nuclearace.github.io/Socket.IO-Client-Swift/Classes/SocketIOClient.html)
  101. - [Manager](https://nuclearace.github.io/Socket.IO-Client-Swift/Classes/SocketManager.html)
  102. - [Engine](https://nuclearace.github.io/Socket.IO-Client-Swift/Classes/SocketEngine.html)
  103. - [Options](https://nuclearace.github.io/Socket.IO-Client-Swift/Enums/SocketIOClientOption.html)
  104. ## Detailed Example
  105. A more detailed example can be found [here](https://github.com/nuclearace/socket.io-client-swift-example)
  106. An example using the Swift Package Manager can be found [here](https://github.com/nuclearace/socket.io-client-swift-spm-example)
  107. ## License
  108. MIT