'Browser Window Session Store Class module
Sub Class_Globals
Private bwWebSocket As WebSocket
Private bwId As String
End Sub
'Initializes the Browser Window Session Store.
'If the browser window has an Id set it get that one otherwise it will generate a new id and set it in the browser window too.
Public Sub Initialize(WebSocketObject As WebSocket)
bwWebSocket = WebSocketObject
bwId = GetBrowserWindowId
If bwId.Length = 0 Then
bwId = GenerateId
SetBrowserWindowId
End If
End Sub
'Initializes the Browser Window Session Store with a specific id and set in the browser window too.
Public Sub Initialize2(WebSocketObject As WebSocket, Id As String)
bwWebSocket = WebSocketObject
bwId = Id
SetBrowserWindowId
End Sub
'Generates an unique identifier
Private Sub GenerateId() As String 'ignore
' If you want to use and UUID then uncomment the 2 lines from below. It makes use of JavaObject library
' Dim jo As JavaObject
' Return jo.InitializeStatic("java.util.UUID").RunMethod("randomUUID", Null)
' If you use UUID (from above) then comment out the line below.
Return Rnd(10, 99) & DateTime.now & Rnd(100, 999)
End Sub
'Sets the Id in the browser window
Private Sub SetBrowserWindowId()
bwWebSocket.Eval("window.name = arguments[0];", Array(bwId))
bwWebSocket.Flush
End Sub
'Get the Id from the browser window
Private Sub GetBrowserWindowId() As String
Dim browserWindowId As Future = bwWebSocket.EvalWithResult("return window.name;", Null) 'ignore
Return browserWindowId.Value
End Sub
'Returns the browser window Id from this class
Public Sub getId() As String
Return bwId
End Sub
'Returns the value tied to the attribute with the specified name.
Public Sub GetAttribute(Name As String) As Object
Return bwWebSocket.Session.GetAttribute(bwId & "_" & Name)
End Sub
'Returns the value tied To the attribute with the specified Name.
'Returns the DefaultValue If no value exists.
Public Sub GetAttribute2(Name As String, DefaultValue As Object) As Object
Return bwWebSocket.Session.GetAttribute2(bwId & "_" & Name, DefaultValue)
End Sub
'Returns a List with all the attribute names.
Public Sub GetAttributesNames() As List
Dim result As List
result.Initialize()
For Each attribute As String In bwWebSocket.Session.GetAttributesNames
If attribute.Length > bwId.Length And attribute.SubString2(0, bwId.Length) = bwId Then
result.Add(attribute.SubString(bwId.Length + 1))
End If
Next
Return result
End Sub
'Tests whether there is a value tied to the given name.
Public Sub HasAttribute(Name As String) As Boolean
Return bwWebSocket.Session.HasAttribute(bwId & "_" & Name)
End Sub
'Removes the attribute with the given name.
Public Sub RemoveAttribute(Name As String)
bwWebSocket.Session.RemoveAttribute(bwId & "_" & Name)
End Sub
'Adds an attribute to the session. If there is an existing value with the same name then it will be replaced.
Public Sub SetAttribute(Name As String, Value As Object)
bwWebSocket.Session.SetAttribute(bwId & "_" & Name, Value)
End Sub
'Invalidates this browser window session store.
'If you chose to preserve attributes in session they will continue to exist in session, otherwise they will be removed.
Public Sub Invalidate(PreserveAttributesInSession As Boolean)
If PreserveAttributesInSession = False Then
For Each attribute As String In bwWebSocket.Session.GetAttributesNames
If attribute.Length > bwId.Length And attribute.SubString2(0, bwId.Length) = bwId Then
bwWebSocket.Session.RemoveAttribute(attribute)
End If
Next
End If
bwId = Null
bwWebSocket = Null
End Sub