shamblett / mqtt5_client

A server and browser based MQTT 5 client for dart
Other
49 stars 25 forks source link

iOS: The connectionState shows connected even when offline #63

Closed aarjan closed 6 months ago

aarjan commented 10 months ago

While i turn the WIFI off, the connectionState still shows connected.

shamblett commented 10 months ago

The client is event driven, if it is connected it will stay connected until an event from the Dart/IOS runtime is received that changes this, e.g. a disconnection event. If no event is received then the client will think it is still connected. The log out put should tell you what is happening when your wifi is turned off.

aarjan commented 10 months ago

@shamblett my concern is that it works out of the box in Android, i.e. connectionState is in not-connected state in Android automatically when wifi/internet is off. But, it still shows connected in iOS.

Here is my logs, ``` flutter: 2023-10-20 14:59:07.277678 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.pingRequest MessageType = MqttMessageType.pingRequest Duplicate = false Retain = false Qos = atMostOnce Size = 0 flutter: 2023-10-20 14:59:07.281809 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>> flutter: 2023-10-20 14:59:51.523223 -- MqttPublishingManager::publish - entered with topic events/-5949569069495159219 flutter: 2023-10-20 14:59:51.535281 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.publish MessageType = MqttMessageType.publish Duplicate = false Retain = false Qos = atLeastOnce Size = 0 Topic Name = events/-5949569069495159219 Message Identifier = 4 Payload Format Indicator = false Message Expiry Interval = 65535 Topic Alias = 255 Response Topic = Subscription Identifier(s) = [] Properties = No properties set Payload: {150 bytes={<123> flutter: 2023-10-20 15:00:02.454023 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>> flutter: ===== 2023-10-20T15:00:05.462516: onEvent flutter: Instance of 'OnComposingStateChanged' flutter: 2023-10-20 15:00:07.443660 -- MqttPublishingManager::publish - entered with topic events/-5949569069495159219 flutter: 2023-10-20 15:00:07.445293 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.publish MessageType = MqttMessageType.publish Duplicate = false Retain = false Qos = atLeastOnce Size = 0 Topic Name = events/-5949569069495159219 Message Identifier = 6 Payload Format Indicator = false Message Expiry Interval = 65535 Topic Alias = 255 flutter: 2023-10-20 15:03:30.596904 -- MqttSynchronousServerConnectionHandler::internalConnect - initiating connection try 1, auto reconnect in progress true flutter: 2023-10-20 15:03:30.596972 -- MqttSynchronousServerConnectionHandler::internalConnect - calling connectAuto flutter: 2023-10-20 15:03:30.597039 -- MqttNormalConnection::connectAuto - entered flutter: 2023-10-20 15:03:30.612171 -- MqttSynchronousServerConnectionHandler::internalConnect exception thrown during auto reconnect - ignoring flutter: 2023-10-20 15:03:30.612348 -- MqttSynchronousServerConnectionHandler::internalConnect - connection complete flutter: 2023-10-20 15:03:30.612425 -- MqttSynchronousServerConnectionHandler::internalConnect sending connect message flutter: 2023-10-20 15:03:30.612491 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.connect MessageType = MqttMessageType.connect Duplicate = false Retain = false Qos = atMostOnce Size = 143 ProtocolName = MQTT ProtocolVersion = 5 ConnectFlags = CleanStart=false, WillFlag=false, WillQos=atMostOnce, WillRetain=false, PasswordFlag=true, UserNameFlag=true KeepAlive = 30 Properties = Identifier : sessionExpiryInterval, value : 4294967295 Will topic = lastwills ```

Also,

  1. Is connectionState is set to disconnected after keepAlive timeout?
  2. Is auto-reconnect triggered after ping fail from keepAlive timeout?
shamblett commented 10 months ago

In answer to your questions -

  1. No
  2. I'm not sure without looking but auto reconnect is being triggered and succeeding -
    2023-10-20 15:03:30.596972 -- MqttSynchronousServerConnectionHandler::internalConnect - calling connectAuto flutter: 2023-10-20 15:03:30.597039 -- MqttNormalConnection::connectAuto - entered flutter: 2023-10-20 15:03:30.612171 -- MqttSynchronousServerConnectionHandler::internalConnect exception thrown during auto reconnect - ignoring flutter: 2023-10-20 15:03:30.612348 -- MqttSynchronousServerConnectionHandler::internalConnect - connection complete 

If the client works as expected on Android but not on IOS then you need to look at IOS, the client doesn't know where its running, it just sits on top of the Dart/flutter runtime. You could compare logs between the two and see what IOS is not doing.

I'm sure other IOS users have seen this(look through the closed issues) and solved it by picking up some kind of IOS system event issued when you turn off wifi and wired this into the disconnect method.