-
Notifications
You must be signed in to change notification settings - Fork 425
CocoaMQTT
MQTT Client
public class CocoaMQTT: NSObject, CocoaMQTTClient
CocoaMQTTClient
, CocoaMQTTDeliverProtocol
, CocoaMQTTReaderDelegate
, CocoaMQTTSocketDelegate
, NSObject
Initial client object
public init(clientID: String, host: String = "localhost", port: UInt16 = 1883, socket: CocoaMQTTSocketProtocol = CocoaMQTTSocket())
- clientID: Client Identifier
- host: The MQTT broker host domain or IP address. Default is "localhost"
- port: The MQTT service port of host. Default is 1883
public weak var delegate: CocoaMQTTDelegate?
public var host = "localhost"
public var port: UInt16 = 1883
public var clientID: String
public var username: String?
public var password: String?
Clean Session flag. Default is true
public var cleanSession = true
Setup a Last Will Message to client before connecting to broker
public var willMessage: CocoaMQTTMessage?
Enable backgounding socket if running on iOS platform. Default is true
public var backgroundOnSocket: Bool
Delegate Executed queue. Default is DispatchQueue.main
public var delegateQueue = DispatchQueue.main
The delegate/closure callback function will be committed asynchronously to it
public var connState = CocoaMQTTConnState.disconnected
Re-deliver the un-acked messages
public var deliverTimeout: Double
Message queue size. default 1000
public var messageQueueSize: UInt
The new publishing messages of Qos1/Qos2 will be drop, if the queue is full
In-flight window size. default 10
public var inflightWindowSize: UInt
Keep alive time interval
public var keepAlive: UInt16 = 60
Enable auto-reconnect mechanism
public var autoReconnect = false
Reconnect time interval
public var autoReconnectTimeInterval: UInt16 = 1
Maximum auto reconnect time interval
public var maxAutoReconnectTimeInterval: UInt16 = 128
The timer starts from autoReconnectTimeInterval
second and grows exponentially until this value
After that, it uses this value for subsequent requests.
Console log level
public var logLevel: CocoaMQTTLoggerLevel
Enable SSL connection
public var enableSSL: Bool
public var sslSettings: [String: NSObject]?
Allow self-signed ca certificate.
public var allowUntrustCACertificate: Bool
Default is false
The subscribed topics in current communication
public var subscriptions: [String: CocoaMQTTQoS] = [:]
public var didConnectAck: (CocoaMQTT, CocoaMQTTConnAck) -> Void
public var didPublishMessage: (CocoaMQTT, CocoaMQTTMessage, UInt16) -> Void
public var didPublishAck: (CocoaMQTT, UInt16) -> Void
public var didReceiveMessage: (CocoaMQTT, CocoaMQTTMessage, UInt16) -> Void
public var didSubscribeTopics: (CocoaMQTT, NSDictionary, [String]) -> Void
public var didUnsubscribeTopics: (CocoaMQTT, [String]) -> Void
public var didPing: (CocoaMQTT) -> Void
public var didReceivePong: (CocoaMQTT) -> Void
public var didDisconnect: (CocoaMQTT, Error?) -> Void
public var didReceiveTrust: (CocoaMQTT, SecTrust, @escaping (Bool) -> Swift.Void) -> Void
public var didCompletePublish: (CocoaMQTT, UInt16) -> Void
public var didChangeState: (CocoaMQTT, CocoaMQTTConnState) -> Void
Connect to MQTT broker
public func connect() -> Bool
- Bool: It indicates whether successfully calling socket connect function. Not yet established correct MQTT session
Connect to MQTT broker
public func connect(timeout: TimeInterval) -> Bool
- timeout: Connect timeout
- Bool: It indicates whether successfully calling socket connect function. Not yet established correct MQTT session
Send a DISCONNECT packet to the broker then close the connection
public func disconnect()
Send a PING request to broker
public func ping()
Publish a message to broker
@discardableResult
public func publish(_ topic: String, withString string: String, qos: CocoaMQTTQoS = .qos1, retained: Bool = false) -> Int
- topic: Topic Name. It can not contain '#', '+' wildcards
- string: Payload string
- qos: Qos. Default is Qos1
- retained: Retained flag. Mark this message is a retained message. default is false
- 0 will be returned, if the message's qos is qos0
- 1-65535 will be returned, if the messages's qos is qos1/qos2
- -1 will be returned, if the messages queue is full
Publish a message to broker
@discardableResult
public func publish(_ message: CocoaMQTTMessage) -> Int
- message: Message
Subscribe a <Topic Name>/<Topic Filter>
public func subscribe(_ topic: String, qos: CocoaMQTTQoS = .qos1)
- topic: Topic Name or Topic Filter
- qos: Qos. Default is qos1
Subscribe a lists of topics
public func subscribe(_ topics: [(String, CocoaMQTTQoS)])
- topics: A list of tuples presented by
(<Topic Names>/<Topic Filters>, Qos)
Unsubscribe a Topic
public func unsubscribe(_ topic: String)
- topic: A Topic Name or Topic Filter
Unsubscribe a list of topics
public func unsubscribe(_ topics: [String])
- topics: A list of
<Topic Names>/<Topic Filters>
public func socketConnected(_ socket: CocoaMQTTSocketProtocol)
public func socket(_ socket: CocoaMQTTSocketProtocol,
didReceive trust: SecTrust,
completionHandler: @escaping (Bool) -> Swift.Void)
public func socketDidSecure(_ sock: GCDAsyncSocket)
public func socket(_ socket: CocoaMQTTSocketProtocol, didWriteDataWithTag tag: Int)
public func socket(_ socket: CocoaMQTTSocketProtocol, didRead data: Data, withTag tag: Int)
public func socketDidDisconnect(_ socket: CocoaMQTTSocketProtocol, withError err: Error?)
Types
- CloseCode
- CocoaMQTT
- CocoaMQTT5
- CocoaMQTT5Message
- CocoaMQTTAUTHReasonCode
- CocoaMQTTCONNACKReasonCode
- CocoaMQTTConnAck
- CocoaMQTTConnState
- CocoaMQTTDISCONNECTReasonCode
- CocoaMQTTError
- CocoaMQTTError.FoundationConnection
- CocoaMQTTLogger
- CocoaMQTTLoggerLevel
- CocoaMQTTMessage
- CocoaMQTTPUBACKReasonCode
- CocoaMQTTPUBCOMPReasonCode
- CocoaMQTTPUBRECReasonCode
- CocoaMQTTPUBRELReasonCode
- CocoaMQTTPropertyName
- CocoaMQTTQoS
- CocoaMQTTSUBACKReasonCode
- CocoaMQTTSocket
- CocoaMQTTUNSUBACKReasonCode
- CocoaMQTTWebSocket
- CocoaMQTTWebSocket.DefaultConnectionBuilder
- CocoaMQTTWebSocket.FoundationConnection
- CocoaMQTTWebSocket.StarscreamConnection
- CocoaRetainHandlingOption
- ErrorType
- FoundationStream
- MqttAuthProperties
- MqttConnectProperties
- MqttDecodeConnAck
- MqttDecodePubAck
- MqttDecodePubComp
- MqttDecodePubRec
- MqttDecodePubRel
- MqttDecodePublish
- MqttDecodeSubAck
- MqttDecodeUnsubAck
- MqttPublishProperties
- MqttSubscription
- PayloadFormatIndicator
- SSLCert
- SSLClientCertificate
- SSLClientCertificateError
- SSLSecurity
- SSLSettings
- WSError
- WebSocket
- WebSocket.OpCode
- WebSocket.WSResponse
- formatInt
Protocols
- CocoaMQTT5Delegate
- CocoaMQTTDelegate
- CocoaMQTTSocketDelegate
- CocoaMQTTSocketProtocol
- CocoaMQTTWebSocketConnection
- CocoaMQTTWebSocketConnectionBuilder
- CocoaMQTTWebSocketConnectionDelegate
- SSLTrustValidator
- WSStream
- WSStreamDelegate
- WebSocketAdvancedDelegate
- WebSocketClient
- WebSocketDelegate
- WebSocketPongDelegate