-
Posted by jesusc on Wed, 14 Nov 2012 11:04:28
Hi,
I need some advice on the best way to handle syncing data across players. I am wanting to sync the most recent timestamp with everyone who's using the addon. Currently what I'm thinking is all players join a hidden chat channel and on an event, sent the current timestamp to the channel, updating all the other players timer. The problem I have is when a player comes online they need to sync back up to the latest timestamp. They would sent a message to the channel requesting an update to which every single addon will respond with their timestamp even though only the most recent one is required.
Is there any way I can prevent this spam and only send the latest timestamp? Is it even a problem if lots of people send their timestamp at the same time?
Thanks.
-
Posted by efindel on Wed, 14 Nov 2012 13:21:34
The traditional way to handle this sort of thing is to have the clients elect a leader. A simple algorithm to do that works like this:
When a client comes online, it records the current time (time from the server, not a timestamp), then sends out a message saying, "Hi, I'm here, who's the leader?"
If there is a leader, the leader replies. It then starts sending data to the new client.
If there is no reply after a certain timeout, the client sends out a message saying, "We need to elect a leader."
When a client receives a "we need to elect a leader" message, it sends out a message containing the time it came online.
The client that just came online examines all the responses, and makes whichever client has been online the longest (i.e., has the oldest server time as the time they came on) the new leader. If there's a tie, it randomly breaks the tie. It sends a message to that client telling it that it is now the leader. The new leader starts sending data to the new client.
If the client receives no responses to its request for an election after a certain timeout, it decides that there are no other clients online, and makes itself the leader.