|
1 | 1 | # ActionCableSwift
|
| 2 | +[](https://swift.org/package-manager/) |
| 3 | +[Action Cable Swift](https://github.com/nerzh/Action-Cable-Swift) is a WebSocket server being released with Rails 5 which makes it easy to add real-time features to your app. This Swift client inspired by "Swift-ActionCableClient", but it not support now and I created Action-Cable-Swift. |
| 4 | + |
| 5 | +### Also web sockets client are now separate from the client. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +To install, simply: |
| 10 | + |
| 11 | +#### Swift Package Manager |
| 12 | + |
| 13 | +Add the following line to your `Package.swift` |
| 14 | + |
| 15 | +```swift |
| 16 | + // ... |
| 17 | + .package(url: "https://github.com/nerzh/Action-Cable-Swift.git", from: "0.1.0") |
| 18 | + // ... |
| 19 | + dependencies: ["ActionCableSwift"] |
| 20 | + // ... |
| 21 | +``` |
| 22 | + |
| 23 | +and you can import ActionCableSwift |
| 24 | + |
| 25 | +```swift |
| 26 | + import ActionCableSwift |
| 27 | +``` |
| 28 | +## Usage |
| 29 | + |
| 30 | +### You will need to implement the `ACWebSocketProtocol` protocol. |
| 31 | + |
| 32 | +### If you use "Starscream", you can take this code or to write your own web socket client: |
| 33 | + |
| 34 | +```swift |
| 35 | +import Foundation |
| 36 | +import Starscream |
| 37 | + |
| 38 | +class WSS: ACWebSocketProtocol, WebSocketDelegate { |
| 39 | + |
| 40 | + var url: URL |
| 41 | + var ws: WebSocket |
| 42 | + |
| 43 | + init(stringURL: String) { |
| 44 | + url = URL(string: stringURL)! |
| 45 | + ws = WebSocket(request: URLRequest(url: url)) |
| 46 | + ws.delegate = self |
| 47 | + } |
| 48 | + |
| 49 | + var onConnected: ((_ headers: [String : String]?) -> Void)? |
| 50 | + var onDisconnected: ((_ reason: String?) -> Void)? |
| 51 | + var onCancelled: (() -> Void)? |
| 52 | + var onText: ((_ text: String) -> Void)? |
| 53 | + var onBinary: ((_ data: Data) -> Void)? |
| 54 | + var onPing: (() -> Void)? |
| 55 | + var onPong: (() -> Void)? |
| 56 | + |
| 57 | + func connect(headers: [String : String]?) { |
| 58 | + ws.request.allHTTPHeaderFields = headers |
| 59 | + ws.connect() |
| 60 | + } |
| 61 | + |
| 62 | + func disconnect() { |
| 63 | + ws.disconnect() |
| 64 | + } |
| 65 | + |
| 66 | + func send(data: Data) { |
| 67 | + ws.write(data: data) |
| 68 | + } |
| 69 | + |
| 70 | + func send(data: Data, _ completion: (() -> Void)?) { |
| 71 | + ws.write(data: data, completion: completion) |
| 72 | + } |
| 73 | + |
| 74 | + func send(text: String) { |
| 75 | + ws.write(string: text) |
| 76 | + } |
| 77 | + |
| 78 | + func send(text: String, _ completion: (() -> Void)?) { |
| 79 | + ws.write(string: text, completion: completion) |
| 80 | + } |
| 81 | + |
| 82 | + func didReceive(event: WebSocketEvent, client: WebSocket) { |
| 83 | + switch event { |
| 84 | + case .connected(let headers): |
| 85 | + onConnected?(headers) |
| 86 | + case .disconnected(let reason, let code): |
| 87 | + onDisconnected?(reason) |
| 88 | + case .text(let string): |
| 89 | + onText?(string) |
| 90 | + case .binary(let data): |
| 91 | + onBinary?(data) |
| 92 | + case .ping(_): |
| 93 | + onPing?() |
| 94 | + case .pong(_): |
| 95 | + onPong?() |
| 96 | + case .cancelled: |
| 97 | + onCancelled?() |
| 98 | + default: break |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +``` |
| 104 | + |
| 105 | + |
| 106 | +```swift |
| 107 | +import ActionCableSwift |
| 108 | + |
| 109 | +/// web socket client |
| 110 | +let ws = WSS(stringURL: "ws://localhost:3334/cable") |
| 111 | + |
| 112 | +/// action cable client |
| 113 | +var client = ACClient(ws: ws) |
| 114 | + |
| 115 | +/// pass headers to connect |
| 116 | +client.headers = ["COOKIE": "Value"] |
| 117 | + |
| 118 | +/// make channel |
| 119 | +/// buffering - buffering messages if disconnect and flush after reconnect |
| 120 | +var options = ACChannelOptions(buffering: true, autoSubscribe: true) |
| 121 | +let channel = client.makeChannel(name: "RoomChannel", options: options) |
| 122 | + |
| 123 | +channel.addOnSubscribe { (channel, optionalMessage) in |
| 124 | + print(optionalMessage) |
| 125 | +} |
| 126 | +channel.addOnMessage { (channel, optionalMessage) in |
| 127 | + print(optionalMessage) |
| 128 | +} |
| 129 | +channel.addOnPing { (channel, optionalMessage) in |
| 130 | + print("ping") |
| 131 | +} |
| 132 | + |
| 133 | +/// Connect |
| 134 | +client.connect() |
| 135 | +``` |
| 136 | + |
| 137 | +### Manual Subscribe to a Channel with Params |
| 138 | + |
| 139 | +```swift |
| 140 | +client.addOnConnected { (h) in |
| 141 | + /// without params |
| 142 | + try? channel.subscribe() |
| 143 | + |
| 144 | + /// with params |
| 145 | + try? channel.subscribe(params: ["Key": "Value"]) |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +### Channel Callbacks |
| 150 | + |
| 151 | +```swift |
| 152 | + |
| 153 | +func addOnMessage(_ handler: @escaping (_ channel: ACChannel, _ message: ACMessage?) -> Void) |
| 154 | + |
| 155 | +func addOnSubscribe(_ handler: @escaping (_ channel: ACChannel, _ message: ACMessage?) -> Void) |
| 156 | + |
| 157 | +func addOnUnsubscribe(_ handler: @escaping (_ channel: ACChannel, _ message: ACMessage?) -> Void) |
| 158 | + |
| 159 | +func addOnRejectSubscription(_ handler: @escaping (_ channel: ACChannel, _ message: ACMessage?) -> Void) |
| 160 | + |
| 161 | +func addOnPing(_ handler: @escaping (_ channel: ACChannel, _ message: ACMessage?) -> Void) |
| 162 | +``` |
| 163 | + |
| 164 | +### Perform an Action on a Channel |
| 165 | + |
| 166 | +```swift |
| 167 | +// Send an action |
| 168 | +channel.addOnSubscribe { (channel, optionalMessage) in |
| 169 | + try? ch.sendMessage(actionName: "speak", params: ["test": 10101010101]) |
| 170 | +} |
| 171 | +``` |
| 172 | + |
| 173 | +### Authorization & Headers |
| 174 | + |
| 175 | +```swift |
| 176 | +client.headers = [ |
| 177 | + "Authorization": "sometoken" |
| 178 | +] |
| 179 | +``` |
| 180 | + |
| 181 | +## Requirements |
| 182 | + |
| 183 | +Any Web Socket Library, e.g. [Starscream](https://github.com/daltoniam/Starscream) |
| 184 | + |
| 185 | +## Author |
| 186 | + |
| 187 | +Me |
| 188 | + |
| 189 | +## License |
| 190 | + |
| 191 | +ActionCableSwift is available under the MIT license. See the LICENSE file for more info. |
2 | 192 |
|
3 |
| -A description of this package. |
|
0 commit comments