B4J Question Using the MqttClient class within a WebSocket

fabton1963

Member
Licensed User
Longtime User
Good morning everyone,

I'm trying to use the MqttClient within a WebSocket to create a simple logger that displays the topics passing through my Mosquitto broker via a web page.

In the 'websocket_connect' event, I invoke the connect function to the MQTT, but I never receive the success or failure event.

Here is my code:
sample:
Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
    ws = WebSocket1
    Main.ValidateUsers(ws)
    Dim str As String = sidemenu.drawSidebar("playerscore")
    dynamicmenu.SetHtml(str)
    BrokerConnectAndReconnect
End Sub

Private Sub WebSocket_Disconnected
    Log("WebSocket_Disconnected MQTTDISCONNECT")
    mqqtcli.Close
End Sub


Private Sub BrokerConnectAndReconnect()
    Dim clientId As String = Rnd(0, 999999999) & DateTime.Now 'create a unique id
    Private MqttIsWorking As Boolean = True
    Do While MqttIsWorking
        If mqqtcli.IsInitialized Then mqqtcli.Close
        Try
            mqqtcli.Initialize("mqtt_log", "tcp://" & Main.settings.Get("MqttDefault"), clientId)
            
            Dim mo As MqttConnectOptions
            mo.Initialize("myuser", "mypassword" )

            mqqtcli.Connect2(mo)
            Wait For mqqtcli (Success As Boolean)    'i set a breakpoint here but is never reached'       
        Catch
            Log(LastException)
        End Try

        If Success Then ' never reached'
            
            Log("Connected to setup broker")
            mqqtcli.Subscribe("#", 0)
            
            Do While MqttIsWorking And mqqtcli.Connected

                Sleep(35000)
            Loop
            
            Log("Disconnected from setup broker")
        Else
            mqqtcli.Close
            Log(LastException)
            Log("Error connecting setup broker")
            
        End If
    Loop
End Sub

Any advice is welcome.
 

fabton1963

Member
Licensed User
Longtime User
Mosquitto is running and my code inserted in main form works perfectly but, if I use it within WebSocket doesn't work, I set the breakpoint beacause in normal mode I nerver receive any message from broker and the code never execute line 34 Log("Connected to setup broker").
 
Upvote 0
Top