B4J Question [SOLVED][ABMaterial] AddHandler

micro

Well-Known Member
Licensed User
Longtime User
Hi to all.
I've created
in ABMApplication page with AddHandler a handler page to receive/send data
from/to a UI application with a simple map.
The problem is that those data are updated every second and the change is
displayed updated only on the last connected device, why?

Thanks
 
Without code, one cannot even guess what the issue could be...
 
Upvote 0
Without code, one cannot even guess what the issue could be...
Ok, in a simple way and reducing the code

In ABMShared
Code: Copy
Sub Process_Globals
   .......
   .......
   'from and for GestVis handler
   Public datovis, notifica As String
   Public eseguito as Boolean
   Public pageobject as Object
End Sub

In ABMApplication
Code: Copy
public Sub StartServer(srvr As Server, srvrName As String, srvrPort As Int)
   .......
   .......
   srvr.AddHandler("/" & ABMShared.AppName & "/gestvis", "ABMGestVis", False)
   .......
End Sub

In Home (startup page where i perform most of the code)
Code: Copy
Public Sub Initialize
    ' build the local structure IMPORTANT!
    BuildPage       
    ABMShared.pageobject = Me
End Sub

In Home.BuilPage and Home.ConnectPage there are the controls for the GUI part (ABMComponent) and variable initialization (In ConnectPage ABMShared.eseguito = True) and this Sub raised from ABMGestVis page.
Code: Copy
Sub SetVis
    ABMShared.eseguito = False
        Dim convis As ABMContainer = page.Cell(1,1).Component("convis")
       Dim lbvis As ABMLabel = convis.Cell(1,1).Component("lbvis")
      lbvis.Text = Paddingstr(ABMShared.datovis, 10)
        ......
        ......
      'perform refresh
       .......
      If ABMShared.notifica <> "" Then
        VisMessageToast(ABMShared.notifica, 2000)
        ABMShared.notifica = ""
    End If
    ABMShared.eseguito = True
End Sub

In ABMGestVis (Handler class)
Code: Copy
Sub Class_Globals
    
End Sub

Public Sub Initialize
    
End Sub

Sub Handle(req As ServletRequest, resp As ServletResponse)
    Dim read As Map
    read.Initialize
    read = ReadObject(req.InputStream)
    If ABMShared.eseguito = False Then Return
    ABMShared.datovis= read.GetDefault("dato", "")
    ABMShared.notifica= read.GetDefault("notifica", "")
        .......
        .......
       CallSubDelayed(ABMShared.pageobject, "SetVis")
End Sub

Sub ReadObject (In As InputStream) As Object
    Try
        Dim out As OutputStream
        out.InitializeToBytesArray(0)
        File.Copy2(In, out)
        Dim raf2 As RandomAccessFile
        raf2.Initialize3(out.ToBytesArray, False)
        Dim res As Map
        res.Initialize
        'res = raf2.ReadB4XObject(0)
        res = raf2.ReadObject(0)
    Catch As Exception
        Log(LastException.Message)
    End Try
    raf2.Close
    Return res
End Sub

The same code with b4j_ws and pure html and CSS with javaobject in websocket class work well, in the sense that every device connected displays correctly the updated data.
I'm sure I'm wrong something in ABM code but I do not know what

Thanks
 
Upvote 0
I think your problem is here:

Code: Copy
ABMShared.pageobject = Me

This means it holds only the value of the LAST Home page. Note that every connection has its own instance of the Home class.

You will need to hold all of the Home page classes and then update all of them when something changes. Take a look at the chat app in de download zip. (especially the ChatShared module which holds all the pages in a map created with Main.srvr.CreateThreadSafeMap). In the ChatPage you see I add every new connection to the map with

Code: Copy
CallSubDelayed3(ChatShared, "NewConnection", Me, ABMPageId)

and when disconnected
Code: Copy
If ABMPageId <> "" Then CallSubDelayed3(ChatShared, "Disconnected", Me, ABMPageId)

So you update then each page:
Code: Copy
For Each page As ChatPage In connections.Values
       CallSubDelayed2(page, "NewMessage", Message)
Next

It is virtually the same concept.
 
Upvote 0
This means it holds only the value of the LAST Home page. Note that every connection has its own instance of the Home class.

You will need to hold all of the Home page classes and then update all of them when something changes. Take a look at the chat app in de download zip.
Thanks alwaysbusy, you refer to the example of chatroom (webapp) by erel?
 
Upvote 0
Back
Top