I am trying to understand what is going on here. I have a B4J application with an mqtt broker created with moquette project broker as seen in the relevant thread in the forum and an other application which is the mqtt client and tries to connect to the broker with credentials defined in MqttConnectOptions ( Private mqttconnopt As MqttConnectOptions ). When the broker runs it connects without any problem. When I try to stop the execution of the broker and try to connect with the following code to catch the case that the broker is unaccessible it never gets to the catch clause and instead probably uses an internal try-catch. Is there a way to make it work the desired way?
B4X:
Public Sub Connect
Try
mqttclient.Connect2(mqttconnopt)
Catch
#if b4a or b4j
If SubExists(oParent, sEvent & "_NotConnected") Then
#End If
#if b4i
If SubExists(oParent, sEvent & "_NotConnected", 0) Then
#End If
CallSub(oParent, sEvent & "_NotConnected")
End If
Log(LastException)
End Try
End Sub
Mqtt connect has a connected event , you can't do try catch, you may put catch block into event mqttclient_Connected(Success As Boolean) and test success for connection
Mqtt connect has a connected event , you can't do try catch, you may put catch block into event mqttclient_Connected(Success As Boolean) and test success for connection
Thanks for the reply. I have tried that already and it works only if the server is online and the client cannot connect to it because of some reason. If the server is not online then the connected event does not work and the code breaks so I tried to catch it in the catch clause with no luck.
The connected event works when the server is not online. you can log LastException
(ConnectException) java.net.ConnectException: Connection refused: connect