#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
Private ActiveConnections As Map
Public SQL As SQL
#Region Database Location
' Private DBLocation As String = "localhost"
Private DBUsername As String = "root"
Private DBPassword As String = "Declan2014!"
#End Region
End Sub
Sub AppStart (Args() As String)
ActiveConnections = srvr.CreateThreadSafeMap
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/avp?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
Public Sub NewConnection(ws As Object)
ActiveConnections.Put(srvr.CurrentThreadIndex, ws)
End Sub
Public Sub RemoveConnection()
ActiveConnections.Remove(srvr.CurrentThreadIndex)
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
' CallSubDelayed(wsHandler, "UpdateDashboardTable")
' Next
'to call all handlers:
Log("Active Connections: " & ActiveConnections)
For Each ws As Object In ActiveConnections.Values
CallSubDelayed (ws, "UpdateDashboardTable")
Next
End Sub