我正在尝试在我的swift应用程序中实现MQTT。我能够连接到Mqtt服务器
[3536:248813] [MOSQUITTO] DEBUG Client iOS_test_mqtt sending PINGREQ
[3536:248813] [MOSQUITTO] DEBUG Client iOS_test_mqtt received PINGRESP
我也订阅了主题,这也有效。但我的问题是没有收到任何从服务器到VC的消息
应用删除
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UINavigationBar.appearance().isTranslucent = true
StartMQTT()
return true
}
func StartMQTT() -> Void {
moscapsule_init()
//set MQTT client configuration
let mqttConfig = MQTTConfig(clientId: "iOS_test_mqtt", host: "192.168.00.00", port: 1883, keepAlive: 60)
mqttConfig.mqttAuthOpts = MQTTAuthOpts(username: "vqsm", password: "test123#")
mqttConfig.onPublishCallback = { messageId in
print("published (msg id=\(messageId)))")
}
mqttConfig.onMessageCallback = { mqttMessage in
print("MQTT Message received: payload=\(mqttMessage.payloadString ?? "no value")")
let receivedMessage = mqttMessage.payloadString!
print("from server msg = \(receivedMessage)")
let data = receivedMessage.data(using: .utf8, allowLossyConversion: false)!
print("xxxxxxx = \(data)")
}
mqttConfig.onSubscribeCallback = { (messageId, grantedQos) in
NSLog("MQTT subscribed (mid=\(messageId),grantedQos=\(grantedQos))")
}
mqttConfig.onPublishCallback = { messageId in
NSLog("published (mid=\(messageId))")
}
mqttClient = MQTT.newConnection(mqttConfig, connectImmediately: true)
}
func subscribeToTopic() -> Void {
let hubId = UserDefaults.standard.value(forKey: "hubID") as? String
let userId = UserDefaults.standard.value(forKey: "user_id") as? String
let Topic :String = "/vq/smart/"+hubId!+"/"+userId!+"/rest/octopus/layout/groups/custom/members"
print("Subring to Topic : \(Topic)")
mqttClient?.subscribe(Topic, qos: 1)
}
func CheckMQTTConnectionStatus() -> Bool {
print("MQTT Connection Status : \(String(describing: mqttClient?.isConnected))")
return (mqttClient?.isConnected)!
}