emqx / CocoaMQTT

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

Connection issues after network being lost and restored #504

Open vadym-kozak opened 1 year ago

vadym-kozak commented 1 year ago

Hi, I have connection issues in case of very bad network connection or even when its being lost and then restored. I did this simulation on iOS using Settings-> Developer -> Network Link Conditioner and switching between different network profiles. Also it is happening in real life when network is unstable. So code is

mqttClient = CocoaMQTT5(clientID: clientID, host: host, port: 8883)
mqttClient?.username = "username"
mqttClient?.password = "pass"
mqttClient?.autoReconnect = true
mqttClient?.enableSSL = true
mqttClient?.keepAlive = 5
mqttClient?.deliverTimeout = 5
mqttClient?.delegate = self
mqttClient?.logLevel = .debug
------Connected here ------

CocoaMQTT(debug): ping
CocoaMQTT(debug): SEND: PING
CocoaMQTT(debug): ==========================MQTT 5.0==========================
CocoaMQTT(debug): packetFixedHeaderType 192
CocoaMQTT(debug): fixedHeader [192]
CocoaMQTT(debug): remainingLen(len: len) [0]
CocoaMQTT(debug): variableHeader []
CocoaMQTT(debug): properties []
CocoaMQTT(debug): payload []
CocoaMQTT(debug): =============================================================
CocoaMQTT(debug): socket wrote data -192
CocoaMQTT(debug): RECV: PONG
pong

------Disabled network here ------

CocoaMQTT(debug): ping
CocoaMQTT(debug): SEND: PING
CocoaMQTT(debug): ==========================MQTT 5.0==========================
CocoaMQTT(debug): packetFixedHeaderType 192
CocoaMQTT(debug): fixedHeader [192]
CocoaMQTT(debug): remainingLen(len: len) [0]
CocoaMQTT(debug): variableHeader []
CocoaMQTT(debug): properties []
CocoaMQTT(debug): payload []
CocoaMQTT(debug): =============================================================
CocoaMQTT(debug): socket wrote data -192
CocoaMQTT(debug): ping
CocoaMQTT(debug): SEND: PING
CocoaMQTT(debug): ==========================MQTT 5.0==========================
CocoaMQTT(debug): packetFixedHeaderType 192
CocoaMQTT(debug): fixedHeader [192]
CocoaMQTT(debug): remainingLen(len: len) [0]
CocoaMQTT(debug): variableHeader []
CocoaMQTT(debug): properties []
CocoaMQTT(debug): payload []
CocoaMQTT(debug): =============================================================
CocoaMQTT(debug): socket wrote data -192
CocoaMQTT(debug): ping
CocoaMQTT(debug): SEND: PING
CocoaMQTT(debug): ==========================MQTT 5.0==========================
CocoaMQTT(debug): packetFixedHeaderType 192
CocoaMQTT(debug): fixedHeader [192]
CocoaMQTT(debug): remainingLen(len: len) [0]
CocoaMQTT(debug): variableHeader []
CocoaMQTT(debug): properties []
CocoaMQTT(debug): payload []
CocoaMQTT(debug): =============================================================
CocoaMQTT(debug): socket wrote data -192
CocoaMQTT(debug): ping
CocoaMQTT(debug): SEND: PING
CocoaMQTT(debug): ==========================MQTT 5.0==========================
CocoaMQTT(debug): packetFixedHeaderType 192
CocoaMQTT(debug): fixedHeader [192]
CocoaMQTT(debug): remainingLen(len: len) [0]
CocoaMQTT(debug): variableHeader []
CocoaMQTT(debug): properties []
CocoaMQTT(debug): payload []
CocoaMQTT(debug): =============================================================
CocoaMQTT(debug): socket wrote data -192

------Enabled network here ------

2022-12-02 10:09:00.777534+0000 App[67721:4873012] [connection] nw_socket_handle_socket_event [C5:1] Socket SO_ERROR [54: Connection reset by peer]
CocoaMQTT(debug): socket disconnected
disconnect

It's never reconnect again

leeway1208 commented 1 year ago

Hello, mqttClient?.autoReconnect = true is only triggered until ConnAck status is not success.

mnich0ls commented 6 months ago

I am also having issues during poor network conditions or when switching from Wi-Fi to Cellular due to poor Wi-Fi signal strength or leaving home/access-point.

The issue I've been seeing is that the connection can get hung in the "connecting" state and the timeout never seems to fire. I've added a work-around by setting my own timer when starting a connection.