CocoaPods trunk is moving to be read-only. Read more on the blog, there are 14 months to go.
| TestsTested | ✓ |
| LangLanguage | SwiftSwift |
| License | MIT |
| ReleasedLast Release | Oct 2016 |
| SPMSupports SPM | ✗ |
Maintained by Yossi Abraham.
| Depends on: | |
| SwiftyJSON | ~> 3.1.0 |
| Starscream | ~> 2.0.0 |
| CryptoSwift | ~> 0.6.0 |
Swamp is a WAMP implementation in Swift.
It currently supports calling remote procedures, subscribing on topics, and publishing events. It also supports authentication using ticket & wampcra authentication.
Swamp utilizes WebSockets as its only available transport, and JSON as its serialization method.
Contributions to support MessagePack & Raw Sockets will be merged gladly!
| Swift Version | Swamp Version | Requirements |
|---|---|---|
| 2.x | 0.1.x | OSX 10.9 or iOS 8.0 |
| 3 | 0.2.0 and above | OSX 10.10 or iOS 8.0 |
Swamp is available through cocoapods. Add
pod 'Swamp', '~> 0.2.0'to your Podfile. (use '~> 0.1.0' for Swift 2)
import Swamp
let swampTransport = WebSocketSwampTransport(wsEndpoint: NSURL(string: "ws://my-router.com:8080/ws")!)
let swampSession = SwampSession(realm: "router-defined-realm", transport: swampTransport)
// Set delegate for callbacks
// swampSession.delegate = <SwampSessionDelegate implementation>
swampSession.connect()
swampSession.disconnect()realm - which realm to jointransport - a SwampTransport implementationauthmethods authid authrole authextra - See your router's documentation and use accordinglyconnect() - Establish transport and perform authentication if configured.disconnect() - Opposite.Now you should wait for your delegate's callbacks:
Implement the following methods:
func swampSessionHandleChallenge(authMethod: String, extra: [String: AnyObject]) -> String
return SwampCraAuthHelper.sign("your-secret", extra["challenge"] as! String) to support wampcra auth method.func swampSessionConnected(session: SwampSession, sessionId: Int)
func swampSessionEnded(reason: String)
reason is usually a WAMP-domain error, but it can also be a textual description of WTF just happened Calling may fire two callbacks:
onSuccess - if calling has completed without errors.onError - If the call has failed. (Either in router or in peer client.)public func call(proc: String, options: [String: AnyObject]=[:], args: [AnyObject]?=nil, kwargs: [String: AnyObject]?=nil, onSuccess: CallCallback, onError: ErrorCallCallback)session.call("wamp.procedure", args: [1, "argument1"],
onSuccess: { details, results, kwResults in
// Usually result is in results[0], but do a manual check in your infrastructure
},
onError: { details, error, args, kwargs in
// Handle your error here (You can ignore args kwargs in most cases)
})session.call("wamp.procedure", options: ["disclose_me": true], args: [1, "argument1"], kwargs: ["arg1": 1, "arg2": "argument2"],
onSuccess: { details, results, kwResults in
// Usually result is in results[0], but do a manual check in your infrastructure
},
onError: { details, error, args, kwargs in
// Handle your error here (You can ignore args kwargs in most cases)
})Subscribing may fire three callbacks:
onSuccess - if subscription has succeeded.onError - if it has not.onEvent - if it succeeded, this is fired when the actual event was published.public func subscribe(topic: String, options: [String: AnyObject]=[:], onSuccess: SubscribeCallback, onError: ErrorSubscribeCallback, onEvent: EventCallback)session.subscribe("wamp.topic", onSuccess: { subscription in
// subscription can be stored for subscription.cancel()
}, onError: { details, error in
}, onEvent: { details, results, kwResults in
// Event data is usually in results, but manually check blabla yadayada
})session.subscribe("wamp.topic", options: ["disclose_me": true],
onSuccess: { subscription in
// subscription can be stored for subscription.cancel()
}, onError: { details, error in
// handle error
}, onEvent: { details, results, kwResults in
// Event data is usually in results, but manually check blabla yadayada
})Publishing may either be called without callbacks (AKA unacknowledged) or with the following two callbacks:
onSuccess - if publishing has succeeded.onError - if it has not.// without acknowledging
public func publish(topic: String, options: [String: AnyObject]=[:], args: [AnyObject]?=nil, kwargs: [String: AnyObject]?=nil)
// with acknowledging
public func publish(topic: String, options: [String: AnyObject]=[:], args: [AnyObject]?=nil, kwargs: [String: AnyObject]?=nil, onSuccess: PublishCallback, onError: ErrorPublishCallback) {session.publish("wamp.topic", args: [1, "argument2"])session.publish("wamp.topic", options: ["disclose_me": true], args: [1, "argument2"], kwargs: ["arg1": 1, "arg2": "argument2"],
onSuccess: {
// Publication has been published!
}, onError: { details, error in
// Handle error (What can it be except wamp.error.not_authorized?)
})For now, only integration tests against crossbar exist. I plan to add unit tests in the future.
In order to run the tests:
Example/Swamp.xcworkspace with XCodeSwamp_Test-iOS or Swamp_Test-OSX
Product -> Test or ⌘U)If for some reason the tests fail, make sure:
/usr/local/bin/docker
You can also inspect Example/swamp-crossbar-instance.log to find out what happened with the crossbar instance while the tests were executing.
I don't care, MIT because it's pod lib create default and I'm too lazy to tldrlegal.