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
 

Harris

Expert
Licensed User
Longtime User
Without code, one cannot even guess what the issue could be...
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Without code, one cannot even guess what the issue could be...
Ok, in a simple way and reducing the code

In ABMShared
B4X:
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
B4X:
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)
B4X:
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.
B4X:
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)
B4X:
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

alwaysbusy

Expert
Licensed User
Longtime User
I think your problem is here:

B4X:
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

B4X:
CallSubDelayed3(ChatShared, "NewConnection", Me, ABMPageId)

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

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

It is virtually the same concept.
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
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
Top