iOS Question socket not responsive on wakeup

Yanik Freeman

Member
Licensed User
Hi there,

I have the code below handling a socket connect/disconnect in B4i. For power-saving measures I call CloseExistingConnection on event Application_Background and reconnect using ConnectToServer on event Application_Foreground. This works when the device is on and the app is in the background and given the focus again but I cannot re-establish the socket when the power button is pressed on the device and put to sleep. Any ideas? Thanks.

Yanik

B4X:
Public Sub ConnectToServer(Host As String)
    Log("Trying to connect to: " & Host)
    CloseExistingConnection
    Dim client As Socket
    client.Initialize("client")
    client.Connect(Host, PORT, 10000)
    Wait For Client_connected (Successful As Boolean)
    If Successful Then
        Log("TCP_connected")
        astream.Initialize(Me, "astream", client.InputStream, client.OutputStream)
        TCP_UpdateState (True)
    Else
        Log("Failed to connect: " & LastException)
        TCP_UpdateState(False)
    End If
End Sub

Public Sub CloseExistingConnection
    If astream.IsInitialized Then astream.Close
    If client.IsInitialized Then client.Close
    TCP_UpdateState (False)
End Sub
 

Yanik Freeman

Member
Licensed User
Re-initializing the server socket on Application_Foreground seems to have done the trick.

B4X:
Dim server As ServerSocket
server.Initialize(PORT, "server")
 
Upvote 1
Top