B4J Question [ JServer] Timer is Async ?

Waldemar Lima

Active Member
Licensed User
hello guys,
i have a question about the "stopwatches", i need to create 100 thousand counters every 10 seconds to be activated asynchronously, if i use the algorithm below will i have any future problems? or do I need to use the "Threading" library?

For each connection I need to initialize an asynchronous Timer .

algorithm :

B4X:
Sub Process_Globals
    Public srvr As Server
    Private threading As Thread 'Need i use this ?
    Type TStopWatch (Identificador As String, cronometro As Timer)
    Dim stopwatch(99) As TStopWatch
End Sub

Sub AppStart (Args() As String)
    srvr.Initialize("srvr")
    srvr.Port = 80
    
    srvr.AddWebSocket("/appserver", "AppServer")
    srvr.AddHandler("/teste", "testes", False)
    DB.Initialize
    DB.Sync
    ServerManager.Initialize
    srvr.Start
    Log("Server Running on Port [80].")
    Log("AppServer Initialized on Port [80/appserver].")
    Log("JdbcConnectionPool Inintialized on [localhost:3306]")

    For i = 0 To stopwatch.Length-1

        stopwatch(i).Initialize
        stopwatch(i).Identificador = "identificador = "&i
        stopwatch(i).cronometro.Initialize("cronometro",1000)
        stopwatch(i).cronometro.Enabled = True

    Next

    StartMessageLoop
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Log("Error = "&Error)
    Log("StackTrace = "&StackTrace)
    Return True
End Sub

Sub cronometro_Tick
    
    'Log("sender = "&Sender)
    
    For i = 0 To stopwatch.Length-1
        
        If (stopwatch(i).cronometro = Sender) Then
            
            Log(stopwatch(i).Identificador & " # at " &DateTime.Time(DateTime.Now))
            Exit
            
        End If
        
    Next
    
End Sub

my fear is that there will be some inconsistency in the timers ...
is it stable?
 

OliverA

Expert
Licensed User
What is the purpose of the timers? What is the goal?
 
Upvote 0

Waldemar Lima

Active Member
Licensed User
What is the purpose of the timers? What is the goal?

I'm creating a multiplayer game (websocket) based on turns, there are 3 players against 3 players, each player has 60 seconds to make 1 move, my idea is for each "room" there is 1 timer to count the 60 seconds of each participant in that room , assuming 3,000 users are connected playing this server, it would be: 500 rooms and 500 timers ( 1 timer per Room )
 
Upvote 0

Waldemar Lima

Active Member
Licensed User
I imagine that by making the server process the events of the match, the game would become more secure because regardless if the user can modify some structure on the client, the server is aware of the true information .
 
Upvote 0

OliverA

Expert
Licensed User
Why not have a timer per websocket connection? You still control the timer on the server side. Since the timer of the websocket handler reports to that sockets event loop, I’m pretty sure the timer is on the thread of the websocket handler and not Main.
 
Upvote 0

Midimaster

Active Member
Licensed User
On the server I would "store" the utc-time (unix timestamp) of last action of a user in a kind a "protocoll". If the user sends again an "action" I would compare the utc-time of the new incoming signal with the last time and decide, whether the difference was <60sec.

Isn't TIMER the wrong solution? As a TIMER is periodically it will tick at 00:00, 01:00, 02:00, 03:00.... But if the user answers the first question after only 30sec at 0:30, he will have again only further 60sec for the next question until 01:30, or? And not 90sec until 2:00, or?
 
Upvote 0

LucaMs

Expert
Licensed User
I'm creating a multiplayer game (websocket) based on turns, there are 3 players against 3 players, each player has 60 seconds to make 1 move, my idea is for each "room" there is 1 timer to count the 60 seconds of each participant in that room
If it's a turn-based game, why have a time for each player? You should have a timer for each room, to be activated every time you pass the turn, or not?
 
Upvote 0

Waldemar Lima

Active Member
Licensed User
There is no such thing: 100K timers. There is no such thing 100K threads.

You can create a single timer per connection. It will not consume any ram.

You can also use a single timer to monitor the state of 1M other things. Let each one store the start time.
I'm trying to create an effective algorithm based on that, thanks: D
 
Upvote 0

Waldemar Lima

Active Member
Licensed User
On the server I would "store" the utc-time (unix timestamp) of last action of a user in a kind a "protocoll". If the user sends again an "action" I would compare the utc-time of the new incoming signal with the last time and decide, whether the difference was <60sec.

Isn't TIMER the wrong solution? As a TIMER is periodically it will tick at 00:00, 01:00, 02:00, 03:00.... But if the user answers the first question after only 30sec at 0:30, he will have again only further 60sec for the next question until 01:30, or? And not 90sec until 2:00, or?

but is it safe for the server to wait for client status?
wouldn't it be better for the game to take place on the server and the client to be just a visual translator?
 
Upvote 0

OliverA

Expert
Licensed User
Another approach could be this:
Use SQLite with the memory database option. When a client gets his turn insert a record with the time the turn started. When the client finishes his turn, remove the record. You could then have a single timer that ticks every second and does a query for all clients whose records have been in the memory database for longer than 60 seconds. Process these clients (stopping their turn, etc). Rinse and repeat.
but is it safe for the server to wait for client status?
With something like the above it would be. Let the client update their timer locally (instead of pushing a page update every second). The client still can't cheat, since the actuall timeout check is done on the server.
 
Upvote 0
Top