Android Question [b4a] mqtt disconnected

jinyistudio

Well-Known Member
Licensed User
Longtime User
Hi

I have a app. copy from example simplechat. I have a question now. normally, I could received mqtt message and show to chat.

Why ? Mqtt client disconnected too when i close chat layout.


B4X:
Public Sub ConnectTo(Host As String, Name As String)
   currentName = Name   
   If connected Then client.Close
   client.Initialize("client", $"tcp://${Host}:${port}"$, "android" & Rnd(1, 10000000))    <<-- initialize
   Dim mo As MqttConnectOptions
   mo.Initialize("", "")
   'this message will be sent if the client is disconnected unexpectedly.
   mo.SetLastWill("all/disconnect", serializator.ConvertObjectToBytes(currentName), 0, False)
   client.Connect2(mo)
End Sub

Private Sub client_MessageArrived (Topic As String, Payload() As Byte)
    Dim receivedObject As Object = serializator.ConvertBytesToObject(Payload)
    If Topic = "all/connect" Or Topic = "all/disconnect" Then
        'new client has connected or disconnected
        Dim newUser As String = receivedObject       
    Else if Topic = "all/users" Then
        Dim newUsers As List = receivedObject
        CallSubDelayed2(Chat, "NewUsers", newUsers) 'this will start the chat activity if it wasn't started yet.
    Else if Topic.IndexOf("iot/plc1/")>=0 Then
        Dim m As MqttData = receivedObject
        Log("topic" & Topic & "," & m)
        CallSubDelayed2(Chat, "NewMessage",m)   <<-- load layout(chat) and show message
        mp.Play
    End If
       
End Sub
 
Top