CocoaMQTT
CocoaMQTT copied to clipboard
It connects when the server is a local Host but the same doesn't works on dedicated IP.
let clientID = "CocoaMQTT-test-“ + String(ProcessInfo().processIdentifier)
let mqtt = CocoaMQTT(clientID: clientID, host: defaultHost, port: 1883)
mqtt!.username = "user1"
mqtt!.password = "password1"
mqtt!.allowUntrustCACertificate = true
mqtt!.willMessage = CocoaMQTTWill(topic: "/will", message: "dieout")
mqtt!.keepAlive = 60
mqtt!.delegate = self
mqtt!.connect()
Same here.
Connecting to localhost
works just fine, but when the host is defined as an IP-address – even if it's the IP-address of localhost! – , it will disconnect immediately with:
Client <unknown> disconnected due to protocol error.
Debug log prints:
CocoaMQTT(info): Connected to 192.168.0.192 : 1883
CocoaMQTT(debug): Socket write message with tag:
Tested on latest 'CocoaMQTT'
and 'CocoaMQTT/WebSockets', '1.3.0-rc.1'
@HJianBo any updates on this one? This problem renders the whole framework virtually unusable 🙁
I just started using this library today. I was getting the same error, ending up on this issue.
Digging around in the code I figured out I shouldn't subscribe to a topic until the didConnectAck
callback occurs. You don't show your code after the connect, might that be what you are doing?
Thanks @mcarland for replying 😀
However, due to that problem described above, I've switched over to another MQTT lib that worked for me. But again, thanks anyway.
@mcarland Sorry to bother you after all this time, but how to you subscribe to a topic after the didConnectAck callback?
@mcarland Sorry to bother you after all this time, but how to you subscribe to a topic after the didConnectAck callback?
Sorry for the delay...
It's been a long time, but here is my connect code. Looks like I assign a callback to didConnectAck
to execute the subscribe.
if mqtt.connect() {
mqtt.didReceiveMessage = { [weak self] (mqtt, message, id) in
self?.process(message)
}
mqtt.didConnectAck = { (mqtt, ack) in
print("connected: \(ack)")
if ack == .accept {
mqtt.subscribe("dev/MyData")
}
}
mqtt.didSubscribeTopic = { (mqtt, topics) in
print("Did subscribe")
}
mqtt.didDisconnect = { (mqtt, error) in
print("MQTT disconnected")
}
} else {
print("did not connect")
}