I am playing with B4jServer to setup a Webapp Dashboard app on my server.
For some reason I cannot get ActiveConnections.Size.
My Main Code:
My Code in Websocket Class Module "DashboardWS":
For some reason I cannot get ActiveConnections.Size.
My Main Code:
B4X:
#Region Project Attributes
#CommandLineArgs:
#MergeLibraries: True
'MySQL Connector/J Driver
#AdditionalJar: mysql-connector-java-5.1.47-bin.jar
#End Region
Sub Process_Globals
Public srvr As Server
Public pool As ConnectionPool
Private LiveTimer As Timer
Public ActiveConnections As List ' Tracks active instances of DashboardWS
Public SQL As SQL
#Region Database Location
' Private DBLocation As String = "localhost"
Private DBUsername As String = "myUsername"
Private DBPassword As String = "myPassword"
#End Region
End Sub
Sub AppStart (Args() As String)
ActiveConnections.Initialize
srvr.Initialize("srvr")
srvr.Port = 5010
' Configure your MySQL connection
' Download driver from: https://mysql.com
Dim driver As String = "com.mysql.cj.jdbc.Driver"
Dim jdbcUrl As String = "jdbc:mysql://localhost:3306/my_db?useSSL=false"
pool.Initialize(driver, jdbcUrl, DBUsername, DBPassword)
' Register the WebSocket handler for the dashboard page
srvr.AddWebSocket("/dashboard/ws", "DashboardWS")
' Start background thread to monitor MySQL changes
StartLiveMonitor
srvr.Start
StartMessageLoop
End Sub
Sub StartLiveMonitor
' Periodically queries MySQL and broadcasts updates to active browsers
Dim t As Timer
t.Initialize("LiveTimer", 10000) ' Poll every 10 seconds
t.Enabled = True
End Sub
Sub LiveTimer_Tick
Log("Timer Tick")
' Loop through all open browser sessions and push live database rows
For i = ActiveConnections.Size - 1 To 0 Step -1
Dim wsHandler As DashboardWS = ActiveConnections.Get(i)
' Use CallSub to safely invoke the class method across threads
CallSub(wsHandler, "UpdateDashboardTable")
Next
Log("Connections: " & ActiveConnections.size)
End Sub
My Code in Websocket Class Module "DashboardWS":
B4X:
'WebSocket class
Sub Class_Globals
Private ws As WebSocket
Private xui As XUI
End Sub
Public Sub Initialize
End Sub
Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
ws = WebSocket1
UpdateDashboardTable
End Sub
Private Sub WebSocket_Disconnected
End Sub
' Call this method whenever the timer detects new data in MySQL
Public Sub UpdateDashboardTable
If ws.Open = False Then Return
Dim sql As SQL = Main.pool.GetConnection
Try
Dim rs As ResultSet = sql.ExecQuery("SELECT * FROM avp.trans ORDER BY id DESC")
Dim sb As StringBuilder
sb.Initialize
Do While rs.NextRow
sb.Append("<tr>")
sb.Append("<td>").Append(rs.GetString("id")).Append("</td>")
sb.Append("<td>").Append(rs.GetString("deviceid")).Append("</td>")
sb.Append("<td><span class='badge'>").Append(rs.GetString("msgtype")).Append("</span></td>")
sb.Append("<td>").Append(rs.GetString("msgdate")).Append("</td>")
sb.Append("</tr>")
Log(rs.GetString("id"))
Loop
rs.Close
' Push the pure HTML rows directly into the table body element
ws.RunFunction("UpdateTableBody", Array As Object(sb.ToString))
ws.Flush
Catch
Log(LastException.Message)
'Finally
sql.Close
End Try
End Sub