使用 Swift 编写的适用于 iOS 和 macOS 的 MQTT
可可MQTT
MQTT v3.1.1 客户端库,适用于使用 Swift 5 编写的 iOS/macOS/tvOS
建
使用 Xcode 11.1 / Swift 5.1 构建
安装
可可豆荚
使用 CocoaPods 进行安装,方法是将以下行添加到 Podfile:
use_frameworks! # Add this if you are targeting iOS 8+ or using Swift
pod 'CocoaMQTT'
然后,运行以下命令:
$ pod install
迦太基
通过将以下行添加到购物车文件,使用迦太基进行安装:
github "emqx/CocoaMQTT" "master"
然后,运行以下命令:
$ carthage update --platform iOS,macOS,tvOS
最后,如果您正在为 OS X 构建:
- 在应用程序目标“常规”设置选项卡上的“嵌入式二进制文件”部分中,将 CocoaMQTT.framework 从磁盘上的 Carthage/Build/Mac 文件夹中拖放。
如果您正在为 iOS 构建,则 tvOS:
-
在应用程序目标的“常规”设置选项卡上的“链接框架和库”部分中,从磁盘上的 Carthage/Build 文件夹中拖放要使用的每个框架。
-
On your application targets “Build Phases” settings tab, click the “+” icon and choose “New Run Script Phase”. Create a Run Script with the following contents:
/usr/local/bin/carthage copy-frameworks
-
and add the paths to the frameworks you want to use under “Input Files”, e.g.:
$(SRCROOT)/Carthage/Build/iOS/CocoaMQTT.framework
Usage
Create a client to connect MQTT broker:
let clientID = "CocoaMQTT-" + String(ProcessInfo().processIdentifier)
let mqtt = CocoaMQTT(clientID: clientID, host: "localhost", port: 1883)
mqtt.username = "test"
mqtt.password = "public"
mqtt.willMessage = CocoaMQTTWill(topic: "/will", message: "dieout")
mqtt.keepAlive = 60
mqtt.delegate = self
mqtt.connect()
Now you can use closures instead of :CocoaMQTTDelegate
mqtt.didReceiveMessage = { mqtt, message, id in
print("Message received in topic \(message.topic) with payload \(message.string!)")
}
SSL Secure
One-way certification
No certificate is required locally. If you want to trust all untrust CA certificates, you can do this:
mqtt.allowUntrustCACertificate = true
Two-way certification
Need a .p12 file which is generated by a public key file and a private key file. You can generate the p12 file in the terminal:
openssl pkcs12 -export -clcerts -in client-cert.pem -inkey client-key.pem -out client.p12
MQTT over Websocket
In the 1.3.0, The CocoaMQTT has supported to connect to MQTT Broker by Websocket.
If you integrated by CocoaPods, you need to modify you like the followings and execute again:Podfile
pod install
use_frameworks!
target 'Example' do
pod 'CocoaMQTT/WebSockets', '1.3.0-rc.2'
end
Then, Create a MQTT instance over Websocket:
let websocket = CocoaMQTTWebSocket(uri: "/mqtt")
let mqtt = CocoaMQTT(clientID: clientID, host: host, port: 8083, socket: websocket)
// ...
_ = mqtt.connect()
If you want to add additional custom header to the connection, you can use the following:
let websocket = CocoaMQTTWebSocket(uri: "/mqtt")
websocket.headers = [
"x-api-key": "value"
]
websocket.enableSSL = true
let mqtt = CocoaMQTT(clientID: clientID, host: host, port: 8083, socket: websocket)
// ...
_ = mqtt.connect()
Example App
You can follow the Example App to learn how to use it. But we need to make the Example App works fisrt:
$ cd Examples
$ pod install
Then, open the by Xcode and start it!Example.xcworkspace/
Dependencies
These third-party functions are used:
- GCDAsyncSocket
- [Starscream](https://github.com/daltoniam/Starscream)
LICENSE
MIT License (see LICENSE
)
Contributors
Author
- Feng Lee [email protected]
- CrazyWisdom [email protected]
- Alex Yu [email protected]