Android Question Websocket closed without event

giada

Member
Hi,
i'm implementing a websocket forever open but sometimes i found it's closed whitout receiving the on_closed event, it's possible? The only way is to implement a periodic control msg?
 

giada

Member
B4X:
Private Sub wshRx_Closed (Reason As String)
    B4XPages.MainPage.MyLog(Main.LIVELLO_DEBUG_WEBSOCKET,"**************************************************************wshRx_Closed " & Reason)
    'websRxConnected = False
End Sub

This sub is called from (in WebSocketHandler):

B4X:
Private Sub ws_Closed (Reason As String)
    CallSub2(CallBack, EventName & "_Closed", Reason)
End Sub

Another timer controls that the connection is true and if is not true connect the websocket

B4X:
Sub timerWebSRx_Tick
    timerWebSRx.Enabled = False

    Select stato_webs_rx
        Case WEBS_CONNECT
            If wshRx.ws.Connected = False Then
                wshRx.Connect("wss://servicetelecare.beghelli.it/ws/Room_" & B4XPages.MainPage.IdTablet)
                stato_webs_rx = WEBS_CONNECTED
                B4XPages.MainPage.Mylog(Main.LIVELLO_DEBUG_WEBSOCKET,"RIAPRO LA WEBS IN RX--------------------------------------")
            End If
            
        Case WEBS_CONNECTED
            If wshRx.ws.Connected = False Then
                stato_webs_rx = WEBS_CONNECT
            else if flagRxMsg = True Then
                If GestisciMessaggi Then
                    B4XPages.MainPage.MyLog(Main.LIVELLO_DEBUG_WEBSOCKET,"Rispondo sulla mia webs")
                    'vuol dire che devo rispondere  sulla mia webs
                    wshRx.SendEventToServer(msgToTx.mitt,msgToTx.dest,msgToTx.cop,msgToTx.progr,msgToTx.data)
                End If
                flagRxMsg = False   
            End If
    End Select
    
    timerWebSRx.Interval = DURATA_TIMER_TX_RX
    timerWebSRx.Enabled = True
End Sub

This code run ok but after times (1 day,one hour...) i find the app that can't receive on the webs...
At the same time i have another webs for trasmission (open tx close) and this is always ok.
I've tried to put a button that in case of problem close the wshRx and so the timer retry to connect..but this not resolve the problem: the webs dn't receive messages...

I think i'll try to re-initiatize the webs in this case before re-connect...
 
Upvote 0

giada

Member
If it's relevant i've implemented the wshRx in a service and my app run on a single-app tablet and is forever in foreground...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Note that if you have a foreground service then you can keep the code in the "pages" or in a class. The pages are never paused.

First step is to add a log message here:
B4X:
Private Sub ws_Closed (Reason As String)
    Log("ws_Closed: " & Reason)
    CallSub2(CallBack, EventName & "_Closed", Reason)
End Sub

Don't expect the WebSocket to be connected and working forever. Better to reconnect automatically every X minutes as all kinds of bad things can happen.
You can see an example in the streamer class here: https://www.b4x.com/android/forum/t...todon-pleroma-social-networks.124214/#content
 
Upvote 0
Top