CocoaMQTT icon indicating copy to clipboard operation
CocoaMQTT copied to clipboard

It connects when the server is a local Host but the same doesn't works on dedicated IP.

Open lets-swapcode opened this issue 5 years ago • 5 comments

	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()

lets-swapcode avatar Jan 09 '20 12:01 lets-swapcode

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 🙁

marckrenn avatar Jul 20 '20 09:07 marckrenn

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?

mcarland avatar Mar 01 '21 21:03 mcarland

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.

marckrenn avatar Mar 02 '21 19:03 marckrenn

@mcarland Sorry to bother you after all this time, but how to you subscribe to a topic after the didConnectAck callback?

GraceStangroome avatar Oct 11 '23 12:10 GraceStangroome

@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")
        }

mcarland avatar Oct 24 '23 23:10 mcarland