Swift mqtt client for ios or osx 【中文介绍】
- Base
Connect
Subscribe
Publish
Unsubscribe
Ping
Disconnect
method. - Clourse-style、asynchronous interface
- Keep alive
- Clean/Recover session state, when connected server
- Support Linux (Ubuntu)
- iOS 8.0+ | macOS 10.10+ | tvOS 9.0+ | watchOS 2.0+
- Xcode 9.x
- Swift 4
Add this dependencies in the Package.swift
// swift-tools-version:4.0
import PackageDescription
let package = Package(
// ...
dependencies: [
.package(url: "https://github.com/HJianBo/Mqtt", from: "0.2.0"),
],
// ...
)
Put the following line to your Cartfile
github "HJianBo/Mqtt"
Then checkout this repos
carthage build
But it will occur compile error because the Mqtt has some others dependencies with Swift Package Manager
There is a workaround as the following:
# Manually checkout SwiftPM dependencies
cd Carthage/Checkouts/Mqtt/ && swift build && cd ../../..
# execute carthage build again
carthage build
come soon..
first, you should import the framework in .swift
file header
import Mqtt
Create a client
let client = MqttClient(clientId: "mqtt-client-t")
Connect to server
client.connect(host: "q.emqtt.com") { address, error in
guard error == nil else {
print("connect failed, error: \(error)")
return
}
print("connect successful! address: \(address)")
}
Subscribe topic with topic filters
client.subscribe(topicFilters: ["topic": .qos1]) { (res, error) in
guard error == nil else {
print("subscribe topic error: \(error)")
return
}
print("subscribe successful! topic authorized result: \(res)")
}
Publish message to some topic
client.publish(topic: "topic", payload: "hi~🙄", qos: .qos2) { error in
guard error == nil else {
print("publish message error: \(error)")
return
}
print("publish message successful!")
}
The more details of the use can refer to the Demo program: Examples/SimpleClient
- In-flight Send Window, Message Ordering.
- Support iOS background model (will disconnect, when lock screen)
- SSL/TLS