emqx / CocoaMQTT

MQTT 5.0 client library for iOS and macOS written in Swift
https://www.emqx.com/en
Other
1.6k stars 419 forks source link

Manual disconnection did not work as expected #465

Closed ckfear closed 2 years ago

ckfear commented 2 years ago

I call mqtt.disconnect,but it still reconnect.I found the variable is_internal_disconnected is always true.

public func disconnect() {
    is_internal_disconnected = false
    internal_disconnect()
}

/// Disconnect unexpectedly
func internal_disconnect() {
    is_internal_disconnected = true
    send(FrameDisconnect(), tag: -0xE0)
    socket.disconnect()
}

And the automatic reconnection logic doesn't look right.

if !autoReconnect{
    guard !is_internal_disconnected else {
        return
    }
}

guard autoReconnect else {
    return
}

The above code makes the is_internal_disconnected variable meaningless.As I understand it, if I disconnect from the outside, it should not automatically reconnect.

ckfear commented 2 years ago

I submitted the pr #466 to fix the problem.

leeway1208 commented 2 years ago

Thank you for your support.😄

leeway1208 commented 2 years ago

Trying to use mqtt!.autoReconnect = false. Is it work?

ckfear commented 2 years ago

It's ok.But I think external disconnection and internal disconnection should be different behaviours. And the parameter is_internal_disconnected should work.

leeway1208 commented 2 years ago

@ckfear Yes. you are right. I misunderstood. Thank you for your contribution!