Name: RxStarscream
Owner: RxSwift Community
Description: A lightweight extension to subscribe Starscream websocket events with RxSwift
Created: 2016-05-18 15:11:50.0
Updated: 2018-05-17 03:04:45.0
Pushed: 2018-03-24 22:31:47.0
Homepage: null
Size: 16653
Language: Swift
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
A lightweight extension to Starscream to track websocket events using RxSwift observables.
RxStarscream is available through CocoaPods.
Add the following line to your Podfile
:
pod 'RxStarscream'
Then run:
pod install
Below is a table that shows which version of RxStarscream you should use for your Swift version.
| Swift | RxStarscream | RxSwift | | —– | ————- |—————| | 4.X | >= 0.8 | >= 4.0 | | 3.X | 0.7 | 3.0.0 - 3.6.1 |
Add this to your Cartfile
github "RxSwiftCommunity/RxStarscream"
Then run:
carthage update
After installing via CococPods or Carthage, you should import the framework.
rt RxStarscream
Once imported, you can open a connection to your WebSocket server.
et = WebSocket(url: URL(string: "ws://localhost:8080/")!)
et.connect()
Now you can subscribe e.g to all of the websocket events:
et.rx.response.subscribe(onNext: { (response: WebSocketEvent) in
switch response {
case .connected:
print("Connected")
case .disconnected(let error):
print("Disconnected with optional error : \(error)")
case .message(let msg):
print("Message : \(msg)")
case .data(_):
print("Data")
case .pong:
print("Pong")
}
isposed(by: disposeBag)
Or just to a connect event:
et.rx.connected.subscribe(onNext: { (isConnected: Bool) in
print("Is connected : \(isConnected)")
isposed(by: disposeBag)
Or just to a message event:
et.rx.text.subscribe(onNext: { (message: String) in
print("Message : \(message)")
isposed(by: disposeBag)
There's a sample project (you need to run carthage update
for it to compile).
The sample project uses echo server - https://www.websocket.org/echo.html
Have fun!
Everyone in the RxSwift Slack channel.
Bug reports and pull requests are welcome.
RxStarscream is available under the MIT license. See the LICENSE file for more info.