Swift 5.0, macOS, Linux。
一个轻量级的 Swift 封装库,用于 libmosquitto。支持 macOS 和 Linux。
使用 SPM。
示例 Package.swift
// swift-tools-version:5.0
import PackageDescription
let package = Package(
name: "ProjectName",
products: [
.executable(name: "ProjectName", targets: ["ProjectName"]),
],
dependencies: [
.package(url: "https://github.com/diejmon/SMosquitto.git", .upToNextMinor(from: "1.4.0")),
],
targets: [
.target(
name: "ProjectName",
dependencies: ["SMosquitto"]),
],
swiftLanguageVersions: [.v5]
)
import SMosquitto
SMosquitto.initialize()
let smosquitto = SMosquitto(id: "An ID", cleanSession: true)
try smosquitto.setLoginInformation(username: "username", password: "password")
try smosquitto.connect(host: "hostname", port: 1883, keepalive: true)
smosquitto.onConnect = { response in
do {
// Subscribe for topics once connected to server.
try smosquitto.subscribe(subscriptionPattern: "atopic", qos: .atleastOnce)
}
catch {
// an error happened
}
}
smosquitto.onMessage = { message in
// Do something with received message
}
/// Start async loop
try smosquitto.loopForever(timeout: .interval(45))
欢迎提交 Pull Request。对于重大更改,请先开一个 issue 以讨论您想要更改的内容。请务必根据需要更新测试。