Compatibility
- 0.3.0 and master5.35.25.15.04.2
- 0.3.0 and masteriOSmacOS(Intel)macOS(ARM)LinuxtvOSwatchOS
Pure swift MQTT client
Swift mqtt client for ios or osx 【中文介绍】
Connect
Subscribe
Publish
Unsubscribe
Ping
Disconnect
method.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