B4J Question safari session variables in b4j v4

billyrudi

Active Member
Licensed User
Longtime User
Hi
i have problems with sessions variables with safari after upgrade to version 4.
it seems that browser lose values.
Chrome and firefox works correctly.
In safari i have the accept cookies and data from all sites active.
can anyone help me?
 
Last edited:

Roberto P.

Well-Known Member
Licensed User
Longtime User
I encountered the same problem also with iPad and iPhone.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've tested the following code with an iPhone:
B4X:
Sub Class_Globals
End Sub

Public Sub Initialize

End Sub

Sub Handle(req As ServletRequest, resp As ServletResponse)
   Dim i As Int = req.GetSession.GetAttribute2("i", 0)
   req.GetSession.SetAttribute("i", i + 1)
     resp.Write(i)
End Sub

The number is incremented. Do you see a different behavior?
 
Upvote 0

billyrudi

Active Member
Licensed User
Longtime User
hi,
in safari and firefox i have problems with session.setattribute.
i have success with
B4X:
WC.LocalStorage_SetItem( "TipoUtente", "paolo")
and
B4X:
 WC.LocalStorage_GetItem( "TipoUtente","null").Value
where functions are
B4X:
Sub LocalStorage_SetItem(Key As String, Value As Object)
    mWS.Eval("$.jStorage.set(arguments[0], arguments[1]);", Array As Object(Key, Value))
End Sub

B4X:
Sub LocalStorage_GetItem(Key As String, Default As Object) As Future
    Return mWS.EvalWithResult("return $.jStorage.get(arguments[0], arguments[1]);", Array As Object(Key, Default))
End Sub
i have a strangeness the jstorage.js must be lowercase.

i have spent two days searching this solution.
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
Erel see your chat example in safari it doesn't works...
Yes, that is the one I suspected it could go wrong. I think it happens when you move to another 'page' on your website. Pre 4.0 could do this, but 4.0 has some troubles saving the session vars. Unfortunately, ABMaterial relies a lot on this technique.

Local storage was indeed also my alternative solution. Maybe it is the new jetty version that plays a role in it?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try the online chat now. It should work (refresh the log-in page to make sure that the cache is invalidated).

The workaround is to create the session and send the cookie before the upgrade request.

This is done by adding this filter:
B4X:
srvr.AddFilter("/b4j_ws.js", "SessionCreator", False)

The class code is:
B4X:
'Return True to allow the request to proceed.
Public Sub Filter(req As ServletRequest, resp As ServletResponse) As Boolean
   req.GetSession 'a new session will be created if a session doesn't exist.
   Return True
End Sub
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
Works perfectly on my iPad Erel, very elegant solution!

For ABMaterial users:

In ABMApplication add in ABMApplication in both StartServer and StartServerHTTP2:

B4X:
' start the server
    srvr.Initialize(srvrName)
    srvr.AddFilter("/js/b4j_ws.js", "ABMSessionCreator", False) ' <----- Add this line
    srvr.AddWebSocket("/ws/" & AppName, "ABMApplication")
...

Create a new Filter class ABMSessionCreator and add:

B4X:
'Filter class
Sub Class_Globals
  
End Sub

Public Sub Initialize
  
End Sub

'Return True to allow the request to proceed.
Public Sub Filter(req As ServletRequest, resp As ServletResponse) As Boolean  
    req.GetSession 'a new session will be created if a session doesn't exist.
    Return True
End Sub
 
Upvote 0
Top