SwiftMQTT 3.0.1

SwiftMQTT 3.0.1

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布上次发布2019年4月
SPM支持 SPM

Adolfo Martinelli 维护。



SwiftMQTT 3.0.1

  • 作者:
  • Ankit Agarwal

SwiftMQTT

MQTT 客户端

Build Status Version License

  • 完全用 Swift 4 编写
  • 强大的错误处理
  • 性能优良
  • 基于 MQTT 3.1.1 规范

使用方法

创建会话

mqttSession = MQTTSession(
	host: "localhost",
	port: 1883,
	clientID: "swift", // must be unique to the client
	cleanSession: true,
	keepAlive: 15,
	useSSL: false
)

连接

mqttSession.connect { error in
    if error == .none {
        print("Connected!")
    } else {
        print(error.description)
    }
}

订阅

let topic = "mytopic" 
mqttSession.subscribe(to: topic, delivering: .atLeastOnce) { error in
    if error == .none {
        print("Subscribed to \(topic)!")
    } else {
        print(error.description)
    }
}

取消订阅

let topic = "mytopic"
mqttSession.unSubscribe(from: topic) { error in
    if error == .none {
        print("Unsubscribed from \(topic)!")
    } else {
        print(error.description)
    }
}

发布

let json = ["key" : "value"]
let data = try! JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
let topic = "mytopic"

mqttSession.publish(data, in: topic, delivering: .atLeastOnce, retain: false) { error in
    if error == .none {
        print("Published data in \(topic)!")
    } else {
        print(error.description)
    }
}

遵守 MQTTSessionDelegate 以接收消息

mqttSession.delegate = self
func mqttDidReceive(message: MQTTMessage, from session: MQTTSession) {
    print(message.topic)
    print(message.stringRepresentation)
}
func mqttDidDisconnect(session: MQTTSession, error: MQTTSessionError) {
    if error == .none {
        print("Successfully disconnected from MQTT broker")
    } else {
        print(error.description)
    }
}

安装

CocoaPods

使用 CocoaPods 安装,请将以下行添加到您的 Podfile 中:

target 'MyApp' do
    use_frameworks!
    pod 'SwiftMQTT'
end

Carthage

github "aciidb0mb3r/SwiftMQTT"

Swift包管理器

dependencies: [
    .package(url: "https://github.com/aciidb0mb3r/SwiftMQTT.git", from: "3.0.0")
]

许可

MIT