EventBus
public class EventBus
Provides a connection to a remote TCP EventBus Bridge.
-
Creates a new EventBus instance.
Note: a new EventBus instance isn’t connected to the bridge automatically. See
connect().Declaration
Swift
public init(host: String, port: Int, pingEvery: Int = 5000)Parameters
hostaddress of host running the bridge
portport the bridge is listening on
pingEveryinterval (in ms) to ping the bridge to ensure it is still up (default:
5000)Return Value
a new EventBus
-
Connects to the remote bridge.
Any already registered message handlers will be (re)connected.
Throws
Socket.Errorif connecting failsDeclaration
Swift
public func connect() throws -
Disconnects from the remote bridge.
Declaration
Swift
public func disconnect() -
Signals the current state of the connection.
Declaration
Swift
public func connected() -> BoolReturn Value
trueif connected to the remote bridge,falseotherwise -
Sends a message to an EventBus address.
If the remote handler will reply, you can provide a callback to handle that reply. If no reply is received within the specified timeout, a
Responsethat responds withfalsetotimeout()will be passed.Throws
Error.invalidData(data:)if the givenbodycan’t be converted to JSONThrows
Error.disconnected(cause:)if not connected to the remote bridgeDeclaration
Swift
public func send(to address: String, body: [String: Any], headers: [String: String]? = nil, replyTimeout: Int = 30000, // 30 seconds callback: ((Response) -> ())? = nil) throwsParameters
tothe address to send the message to
bodythe body of the message
headersheaders to send with the message (default:
[String: String]())replyTimeoutthe timeout (in ms) to wait for a reply if a reply callback is provided (default:
30000)callbackthe callback to handle the reply or timeout
Response(default:nil) -
Publishes a message to the EventBus.
Throws
Error.invalidData(data:)if the givenbodycan’t be converted to JSONThrows
Error.disconnected(cause:)if not connected to the remote bridgeDeclaration
Swift
public func publish(to address: String, body: [String: Any], headers: [String: String]? = nil) throwsParameters
tothe address to send the message to
bodythe body of the message
headersheaders to send with the message (default:
[String: String]()) -
Registers a closure to receive messages for the given address.
Throws
Error.disconnected(cause:)if not connected to the remote bridgeDeclaration
Swift
public func register(address: String, id: String? = nil, headers: [String: String]? = nil, handler: @escaping (Message) -> ()) throws -> StringParameters
addressthe address to listen to
idthe id for the registration (default: a random uuid)
headersheaders to send with the register request (default:
[String: String]())handlerthe closure to handle each
MessageReturn Value
an id for the registration that can be used to unregister it
-
Registers a closure to receive messages for the given address.
Throws
Error.disconnected(cause:)if not connected to the remote bridgeDeclaration
Swift
public func unregister(address: String, id: String, headers: [String: String]? = nil) throws -> BoolParameters
addressthe address to remove the registration from
idthe id for the registration
headersheaders to send with the unregister request (default:
[String: String]())Return Value
trueif something was actually unregistered -
Registers an error handler that will be passed any errors that occur in async operations.
Operations that can trigger this error handler are:
- handling messages received from the remote bridge (can trigger
Error.disconnected(cause:)orError.serverError(message:)) the ping operation discovering the bridge connection has closed (can trigger
Error.disconnected(cause:))
Declaration
Swift
public func register(errorHandler: @escaping (Error) -> ())Parameters
errorHandlera closure that will be passed an
Errorwhen an error occurs - handling messages received from the remote bridge (can trigger
-
Represents error types thrown or passed by EventBus methods.
See moreDeclaration
Swift
public enum Error : Swift.Error
View on GitHub
Install in Dash
EventBus Class Reference