在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):emqx/CocoaMQTT开源软件地址(OpenSource Url):https://github.com/emqx/CocoaMQTT开源编程语言(OpenSource Language):Swift 99.4%开源软件介绍(OpenSource Introduction):CocoaMQTTMQTT v3.1.1 and v5.0 client library for iOS/macOS/tvOS written with Swift 5 BuildBuild with Xcode 11.1 / Swift 5.1 IOS Target: 9.0 or above OSX Target: 10.12 or above TVOS Target: 10.0 or above InstallationCocoaPodsTo integrate CocoaMQTT into your Xcode project using CocoaPods, you need to modify you use_frameworks!
target 'Example' do
pod 'CocoaMQTT'
end Then, run the following command: $ pod install At last, import "CocoaMQTT" to your project: import CocoaMQTT CarthageInstall using Carthage by adding the following lines to your Cartfile:
Then, run the following command: $ carthage update --platform iOS,macOS,tvOS --use-xcframeworks At last: On your application targets “General” settings tab, in the "Frameworks, Libraries, and Embedded content" section, drag and drop CocoaMQTT.xcframework, CocoaAsyncSocket.xcframework and Starscream.xcframework from the Carthage/Build folder on disk. Then select "Embed & Sign". UsageCreate a client to connect MQTT broker: ///MQTT 5.0
let clientID = "CocoaMQTT-" + String(ProcessInfo().processIdentifier)
let mqtt5 = CocoaMQTT5(clientID: clientID, host: "localhost", port: 1883)
let connectProperties = MqttConnectProperties()
connectProperties.topicAliasMaximum = 0
connectProperties.sessionExpiryInterval = 0
connectProperties.receiveMaximum = 100
connectProperties.maximumPacketSize = 500
mqtt5.connectProperties = connectProperties
mqtt5.username = "test"
mqtt5.password = "public"
mqtt5.willMessage = CocoaMQTTWill(topic: "/will", message: "dieout")
mqtt5.keepAlive = 60
mqtt5.delegate = self
mqtt5.connect()
///MQTT 3.1.1
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 mqtt.didReceiveMessage = { mqtt, message, id in
print("Message received in topic \(message.topic) with payload \(message.string!)")
} SSL SecureOne-way certificationNo certificate is required locally. If you want to trust all untrust CA certificates, you can do this: mqtt.allowUntrustCACertificate = true Two-way certificationNeed 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:
MQTT over WebsocketIn 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 use_frameworks!
target 'Example' do
pod 'CocoaMQTT/WebSockets', '1.3.0-rc.2'
end If you're using CocoaMQTT in a project with only a Pod::Spec.new do |s|
...
s.dependency "Starscream", "~> 3.1.1"
end Then, Create a MQTT instance over Websocket: ///MQTT 5.0
let websocket = CocoaMQTTWebSocket(uri: "/mqtt")
let mqtt5 = CocoaMQTT5(clientID: clientID, host: host, port: 8083, socket: websocket)
let connectProperties = MqttConnectProperties()
connectProperties.topicAliasMaximum = 0
// ...
mqtt5.connectProperties = connectProperties
// ...
_ = mqtt5.connect()
///MQTT 3.1.1
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 AppYou 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 DependenciesThese third-party functions are used:
LICENSEMIT License (see ContributorsAuthor
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论