我制作了一个应用程序,通过MQTT服务器接收来自我的覆盆子pi的消息,但是当我启动应用程序时,应用程序立即断开连接并且无法接收任何消息。
import Moscapsule
var input: String = ""
class ViewController: UIViewController {
// set MQTT Client Configuration
let mqttConfig = MQTTConfig(clientId: "iOS Device", host: "192.168.178.27", port: 1883, keepAlive: 10000)
override func viewDidLoad() {
super.viewDidLoad()
mqttConfig.onConnectCallback = { returnCode in
NSLog("Return Code is \(returnCode.description)")
}
mqttConfig.onMessageCallback = { mqttMessage in
NSLog("MQTT Message received: payload=\(String(describing: mqttMessage.payloadString))")
input = mqttMessage.payloadString!;
print(input);
}
// create new MQTT Connection
let mqttClient = MQTT.newConnection(mqttConfig)
// publish and subscribe
mqttClient.publish(string: "message", topic: "RPI3B/iOS", qos: 2, retain: false)
mqttClient.subscribe("RPI3B/iOS", qos: 2)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
这是我的代码
1513018389: New client connected from 192.168.178.13 as iOS Device (c1, k10000).
1513018389: Sending CONNACK to iOS Device (0, 0)
1513018389: Received PUBLISH from iOS Device (d0, q2, r0, m1, 'RPI3B/iOS', ... (7 bytes))
1513018389: Sending PUBREC to iOS Device (Mid: 1)
1513018389: Received SUBSCRIBE from iOS Device
1513018389: RPI3B/iOS (QoS 2)
1513018389: iOS Device 2 RPI3B/iOS
1513018389: Sending SUBACK to iOS Device
1513018389: Received DISCONNECT from iOS Device
1513018389: Client iOS Device disconnected.
这是我的MQTT服务器在我的覆盆子PI上显示的内容
2017-12-11 19:53:09.863392+0100 MSQTTtest[9620:739633] [MOSQUITTO] DEBUG Client iOS Device sending CONNECT
2017-12-11 19:53:09.864524+0100 MSQTTtest[9620:739636] [MOSQUITTO] DEBUG Client iOS Device sending PUBLISH (d0, q2, r0, m1, 'RPI3B/iOS', ... (7 bytes))
2017-12-11 19:53:09.864924+0100 MSQTTtest[9620:739633] [MOSQUITTO] DEBUG Client iOS Device sending SUBSCRIBE (Mid: 2, Topic: RPI3B/iOS, QoS: 2)
2017-12-11 19:53:09.865260+0100 MSQTTtest[9620:739633] [MOSQUITTO] DEBUG Client iOS Device sending DISCONNECT
这是我的控制台在Xcode上显示的内容