Android Question On EventMqtt_Connected (Success As Boolean) - Success = True and Mqtt.connected = False ?

amorosik

Expert
Licensed User
I am using the classic example to communicate via mqtt, and seeing in the EventMqtt_Connected(...) routine I would expect to have the agreement between the Success variable and the Connected property of the mqtt client, but it is not so

2021-10-09 17:51:43 410 - Mqtt.EventMqtt_Connected - Success = true - Mqtt_client.Connected = false

How is it possible that the Success variable (of EventMqtt_Connected sub) is True while the mqtt_client.connected property is False?



B4X:
Private Sub EventMqtt_Connected (Success As Boolean)
    Log("Mqtt.EventMqtt_Connected - Success = " & Success & " - Mqtt_client.Connected=" & mqtt_client.Connected)
   
    Try
    If Success Then
            mqtt_client.Subscribe("consolle_to_applauncher", 0)
            mqtt_client.Publish2("applauncher_to_consolle", serializator.ConvertObjectToBytes(stringa_connected), 0, False)
           End If
    Catch
        Log(LastException)
    End Try
End Sub

Log Windows:
2021-10-09 17:51:40 407 - Mqtt.EventMqtt_Connected - Success = true - Mqtt_client.Connected=true
2021-10-09 17:51:40 410 - Mqtt.EventMqtt_Connected: true
2021-10-09 17:51:40 771 Timer1sec_tick Mqtt Link: true
2021-10-09 17:51:41 379 - Mqtt.EventMqtt_Disconnected
2021-10-09 17:51:41 383 - Mqtt.EventMqtt_Connected - Success = true - Mqtt_client.Connected=false
2021-10-09 17:51:41 386 - Mqtt.EventMqtt_Connected: true
(MqttException) Client non connesso (32104)
2021-10-09 17:51:41 772 Timer1sec_tick Mqtt Link: false
2021-10-09 17:51:42 773 Timer1sec_tick Mqtt Link: false
2021-10-09 17:51:43 349 - Mqtt.Timer5sReconnectMqttEvent_Tick  mqtt_client.Connected=false
2021-10-09 17:51:43 351 - Mqtt.ConnectTo: tcp://1.2.3.4:63309 123456789
2021-10-09 17:51:43 376 - Mqtt.EventMqtt_Disconnected
2021-10-09 17:51:43 395 - Mqtt.EventMqtt_Connected - Success = true - Mqtt_client.Connected=true
2021-10-09 17:51:43 401 - Mqtt.EventMqtt_Connected: true
(MqttException) Client non connesso (32104)
2021-10-09 17:51:43 407 - Mqtt.EventMqtt_Disconnected
2021-10-09 17:51:43 410 - Mqtt.EventMqtt_Connected - Success = true - Mqtt_client.Connected=false
2021-10-09 17:51:43 413 - Mqtt.EventMqtt_Connected: true
(MqttException) Client non connesso (32104)
2021-10-09 17:51:43 774 Timer1sec_tick Mqtt Link: false
2021-10-09 17:51:44 775 Timer1sec_tick Mqtt Link: false
2021-10-09 17:51:45 775 Timer1sec_tick Mqtt Link: false
2021-10-09 17:51:46 350 - Mqtt.Timer5sReconnectMqttEvent_Tick  mqtt_client.Connected=false
2021-10-09 17:51:46 352 - Mqtt.ConnectTo: tcp://1.2.3.4:63309 123456789
2021-10-09 17:51:46 377 - Mqtt.EventMqtt_Disconnected
2021-10-09 17:51:46 396 - Mqtt.EventMqtt_Connected - Success = true - Mqtt_client.Connected=true
2021-10-09 17:51:46 399 - Mqtt.EventMqtt_Connected: true
(MqttException) Client non connesso (32104)
2021-10-09 17:51:46 409 - Mqtt.EventMqtt_Disconnected
2021-10-09 17:51:46 411 - Mqtt.EventMqtt_Connected - Success = true - Mqtt_client.Connected=false
 

amorosik

Expert
Licensed User
It can happen if the connection was immediately broken. The event is raised from a different thread so there could be a few milliseconds between the time the connection was established and the time the event was executed.

You mean that the event "EventMqtt_Connected" can be triggered even if the "mqtt_client.Connected" property is False, then after few millisecond also "mqtt_client.Connected" is set to True?
Than "mqtt_client.Connected" properties is not ALWAYS return the real connection state of mqtt channel?
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Than "mqtt_client.Connected" properties is not ALWAYS return the real connection state of mqtt channel?
Maybe related with this post?

Have you added the line number six to the "ConnectAndReconnect" Sub?

B4X:
 Sleep(100)
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Just for testing, have you try with the tcp://test.mosquitto.org server?
 
Upvote 0
Top