How to prevent 2 users or more from joining to a room in the same millisecond

Need help with SmartFoxServer? You didn't find an answer in our documentation? Please, post your questions here!

Moderators: Lapo, Bax

MrPencen
Posts: 5
Joined: 31 Jul 2021, 17:58

How to prevent 2 users or more from joining to a room in the same millisecond

Postby MrPencen » 09 Aug 2021, 13:30

Hello,

in my project there is a room called ( competition room ) in which users can join it to do some competitions

when one user join alone he first has to wait in ( waiting screen for another users to start the room with them)

the maximum number of users that can join to the same room is 4

when one user join the room alone, he has to wait for at least another one to join with him and when there is another one join him, there is a counter (timer) that starts from 15 to 0 ( to wait if there will be another users to join them or not, as I said before the maximum number is 4) I have made it by using (set interval and clear interval )

So if one user join the room alone, he waits for another one.
when another one join him in the same room, The counter starts from 15 to 0
if 3rd and 4th users join them before the end of the counter, they will take the same counter ( there is no counter created if there is already one) so if 3rd user for example join them when there is only 7 seconds he will start with them from 7 not 15

I hope it's clear


All of that works correctly but there is only on problem which occurs rarely but it occurs

when one user join the room alone, and then another 2 users join to him ( at the same millisecond ) there are 2 timers start because the the 2nd user and 3rd user join the room at the same millisceond

I have noticed that by using

Code: Select all

trace(params.countDown)



The problem is when it become 0 in the both timers there is only one timer become destroyed by clear interval and the other one continue as -1, -2 ,-3, -4 .... and the users don't start the competitions even if them exit from the room and join again because the second timer which not destroyed still works unless I restart the server

My interpretation of the problem: that when the both players joined the first player at the same millisecond, the server deals with them as both are the 2nd player , so two counters are created

when this problem occur I have to restart the server to work again


How can I solve it? the problem is not from my code as I said it occurs only when 2 users join the same room at the same milliseconds which is very rarely I know but it occurs ( if there is difference 1 millisecond or more between the joining of 2nd and 3rd the problem doesn't occur )

I tried to repeat (clear interval) twice but it still destroy only one timer

So I need to make someone delay only for a 1 millisecond if he join at the millisecond with another one
User avatar
Lapo
Site Admin
Posts: 23007
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: How to prevent 2 users or more from joining to a room in the same millisecond

Postby Lapo » 11 Aug 2021, 15:32

Hi,
thanks for the detailed explanation.
It sounds like it's a concurrency issue, where two timers are run at the same time because two join events happen very close in time and there is no mutual exclusion lock to prevent it.

What is that? It is a mechanism to safeguard operations that run simultaneously in two separate threads and that should run in sequence.
Is your server side code written in Java or Javascript?

Thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
MrPencen
Posts: 5
Joined: 31 Jul 2021, 17:58

Re: How to prevent 2 users or more from joining to a room in the same millisecond

Postby MrPencen » 12 Aug 2021, 03:43

Hello Mr Lapo, at first sorry for disturbing you

Am using: Server-side ActionScript API

As I said before the problem occurs because when 2 users join the room in the same millisecond it leads to 2 timers start ( Am using setinterval for timer)

if we can't prevent them from joining at same millisecond

What about clearing the 2 intervals?

When I use " clear interval" it only clears one interval and the other one continue

knowing that I used "trace(myinterval)" and it showed me that the 2 intervals are exactly the same

So how can I clear both of them?

that's my code

Code: Select all

  var params = {}
  params.countDown  = 15
  params.Username   = Username
  params.SFS_ID     = SFS_ID
  myInterval = setInterval("TimerCounter", 1000, params)


and that's TimerCounter:

Code: Select all

function TimerCounter(params)
{
   params.countDown--
   
      if (params.countDown == 0) {

   clearInterval(myInterval)
        startGame()
}
}


Notice " startGame is a function which is responsible for starting the competition after the end of timer"
User avatar
Lapo
Site Admin
Posts: 23007
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: How to prevent 2 users or more from joining to a room in the same millisecond

Postby Lapo » 23 Aug 2021, 14:31

Sorry for the delay...
As I said before the problem occurs because when 2 users join the room in the same millisecond it leads to 2 timers start ( Am using setinterval for timer)

if we can't prevent them from joining at same millisecond

What about clearing the 2 intervals?

From what I understand the problem is to avoid creating 2 timers, rather than trying to deal with them. This could be done by creating a Java lock and avoid that two threads interfere with each others when this particular situation happens (two users joining almost at the same time)

If you're not familiar with Java and multithreading you can read this:
https://www.baeldung.com/java-concurrent-locks

The Java Lock class I am referring to is this:
https://docs.oracle.com/javase/7/docs/a ... tLock.html

Also in section 6.6 of the SFS PRO documentation you find examples of how to integrate java classes in Javascript:
https://www.smartfoxserver.com/docs/1x/

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SmartFoxServer 1.x Discussions and Help”

Who is online

Users browsing this forum: No registered users and 34 guests