B4J Question [ABMaterial] How to reconnect without duplicating content

stanmiller

Active Member
Licensed User
Longtime User
It appears I'm missing a step when the browser reconnects to the ABM webapp after a timeout. The app is running under ABM 2.20.

This morning I opened my phone to this.

1_abm_refresh_zpspr1ba2ya.jpg


Ideas?
 

alwaysbusy

Expert
Licensed User
Longtime User
This is normal behaviour. At some point the cache is empty on the server, but the browser is still open so the Sync between the server and what is in the browser is completely lost. You have to catch this yourself.

What are the settings of ABMShared.SessionMaxInactiveIntervalSeconds? (In my apps I set it to 3 days as it are inhouse apps)

If for example this is set to 30 minutes, then whatever happens, if the device was not able to do a heartbeat (every 30/2 minutes) because the internet connection was lost or the user closed the browser, at that point the server will kill the session after 30 minutes and the scavenger will delete the cache.

If the user now reconnects (after the 30 minutes), you'll need to reload the page (or in my apps I send it back to the entry point of the WebApp).

You can do this by putting something in the session.

e.g. I use this in WebSocket_connected BEFORE ABM.UpdateFromCache(Me, ABMShared.CachedPages, ABMPageId, ws):

B4X:
If session.GetAttribute2("IsAuthorized2020", "") = "" Then
       ABMShared.NavigateToPage(ws, ABMPageId, "../",False)  ' go back to the apps entry point.
       Return
End If

In the ABMApplication, in WebSocket_connection, I set this variable (in my case the user needs to login, but if this is not the case for you, just set the variable)

B4X:
session.SetAttribute("IsAuthorized2020", "true")

So if the session is killed by jServer, the variable IsAuthorized2020 is gone, so it goes to the entry point of my app.
 
Upvote 0

stanmiller

Active Member
Licensed User
Longtime User
Thanks. That worked!

What are the settings of ABMShared.SessionMaxInactiveIntervalSeconds? (In my apps I set it to 3 days as it are inhouse apps)

The timeouts are set to the defaults. 30 minutes for the session and 15 minutes for the scavenger.

The app uses a session variable for the yard location. I added a check for the session variable in WebSocket_Connected as advised.

B4X:
'------------------------------------------------
' Operations
'------------------------------------------------

Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
    Log("ABM: Connected")    
    ws = WebSocket1
    ABMPageId = ABM.GetPageID(page, Name,ws)
    Dim session As HttpSession = ABM.GetSession(ws, ABMShared.SessionMaxInactiveIntervalSeconds)

    If ABMShared.NeedsAuthorization Then
        If session.GetAttribute2("IsAuthorized", "") = "" Then
            ABMShared.NavigateToPage(ws, ABMPageId, "../")
            Return
        End If
    End If

    ' If session location empty re-navigate to the page
    If ( session.GetAttribute2("Location", "" ) = "" ) Then
        Mtelog.Console( "Siteone: Reloading page..." )
        ws.Session.SetAttribute("Location", sLocationCode )
        ABMShared.NavigateToPage(ws, ABMPageId, TARGET_PAGE )
        Return    
    End If

    ABM.UpdateFromCache(Me, ABMShared.CachedPages, ABMPageId, ws)
    If page.ComesFromPageCache Then
        ' refresh the page
        page.Refresh
        ' Tell the browser we finished loading
        page.FinishedLoading
    Else
        ' Prepare the page
        page.Prepare
        ' load the dynamic content
        ConnectPage
    End If
    Log("ABM: Connected. ABMPageId=" & ABMPageId)        
End Sub

Private Sub WebSocket_Disconnected
    Log("ABM: Disconnected")
End Sub
 
Upvote 0
Top