Android Question WebSocket Activity_Resume

web-yacht

Member
Licensed User
Longtime User
I'm using WebSocket library 2.10, everything works, send message to server, receive,...

I have a problem when the app is paused and then resumed.
I close the websocket connection in Activity_Pause, server side i see the client disconnected ok,
and then i open the connection in Activity_Resume,
everything works if the app is resumed by tapping the app icon,
but does not work if app is resumed from "recent apps". In this case the websocket seems already connected
and if i try to reconnect i receive this error:

io.crossbar.autobahn.websocket.WebSocketException: already connected

Activity_Pause and Activity_Resume
If resumed from "recent apps" wsh.ws.Connected is true !?!
B4X:
Sub Activity_Resume
    'controlla connessione wsh
    Log("resumed wshconnect:" & wsh.ws.Connected)
    wsh.Connect(WY_ws)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If wsh.IsInitialized = True Then
        wsh.Close
    End If
End Sub
B4X:

Here i added ws.Connected check to avoid above error
B4X:
Public Sub Connect(Url As String)
    If ws.Connected=False Then
        ws.Connect(Url)  
        Log("ws.Connect " & Url)
    End If
End Sub
 

DonManfred

Expert
Licensed User
Longtime User
Did you try to disconnect in case ws.Connected is true?

Untested code:

B4X:
Public Sub Connect(Url As String)
    if ws.Connected then
        ws.Disconnect
        sleep(250)
    end if
    If ws.Connected=False Then
        ws.Connect(Url)  
        Log("ws.Connect " & Url)
    End If
End Sub

Away from that you should move all the communication code to the starter service
 
Upvote 0
Top