Server time on Client or server client time synchronisation

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

User avatar
Avinash
Posts: 3
Joined: 20 Apr 2017, 10:46

Server time on Client or server client time synchronisation

Postby Avinash » 20 Apr 2017, 12:09

Hi,

I am using smart fox server with Unity client for our new game (a Card game). I have already built 2 multiplayer games which are live (with more than 20million downloads). We used Photon for these games. I find smart fox really interesting and best part is server side control using extensions. There are few things i am struggling with which i searched everywhere but didn't find any answer.

Is there a server time synchronised with Client? If not then can you suggest whats the best way to do it or any existing implementation. This is a must have feature for any multiplayer game. Based on this server can tell client to perform any action at a particular time (which is same on both sides).
Last edited by Avinash on 21 Apr 2017, 06:23, edited 1 time in total.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Server time on Client or server client time synchronisation

Postby Lapo » 20 Apr 2017, 13:55

Hello,
Avinash wrote:Is there a server time synchronised with Client? If not then can you suggest whats the best way to do it or any existing implementation.
This is a must have feature for any multiplayer game. Based on this server can tell client to perform any action at a particular time (which is same on both and many more).

One way to do it is using a Scheduler. You schedule a task to be executed in 30 seconds (for instance). When it's time the server-side will send a command to the client which will execute a certain action.

You can run hundreds of concurrent tasks within the same scheduler. Take a look here:
http://docs2x.smartfoxserver.com/Gettin ... wtos#item7

Is this what you mean? If not can you clarify?

Thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
Avinash
Posts: 3
Joined: 20 Apr 2017, 10:46

Re: Server time on Client or server client time synchronisation

Postby Avinash » 21 Apr 2017, 06:15

Hi Lapo,

Thanks for quick reply. This was my first post and i am impressed with your support.

Please don't confuse it with Scheduler. I have read in forum and many people confused it with Scheduler. Let me explain.

I want something like SmartFoxServer.time (same on all clients). The idea is, this can be server time which is synced across all clients.

Now server wants all client to, for example start game after 10 seconds from this global time. Server sends a command to all clients saying
start game at SmartFoxServer.time + 10s. All clients receive this command after their own delay (Client 1 after 500ms, Client 2 after 1000ms and so on, assuming bad network).

No matter what lag all clients have they all know when exactly the game is starting and they can show a count down or something till GameStartTime (everyone has to just check there own SmartFoxServer.time).

Now there are ways to do it. I can use global timestamp both on server and clients. Which are always same. But i don't want to trust client timestamp.

Code: Select all

System.currentTimeMillis()/1000  //on server (JAVA)

(Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds // On Unity client (c#)


Other way is i send server timestamp to clients then clients use this (after adding lag) and keep adding Time.deltaTime till its again overridden from server. Send server timestamp with all requests from server ( No need to schedule a repeating task to update this).

Did you get what i am trying to do? Is there something like this already present in smartfox. Do you send server timestamp with request header (Didn't find in docs).

This can really be useful in many scenarios and reduces the need of schedulers for most of the things.
User avatar
meanvel
Posts: 129
Joined: 19 Jan 2017, 14:06

Re: Server time on Client or server client time synchronisation

Postby meanvel » 21 Apr 2017, 07:10

Avinash wrote:Hi Lapo,

Thanks for quick reply. This was my first post and i am impressed with your support.

Please don't confuse it with Scheduler. I have read in forum and many people confused it with Scheduler. Let me explain.

I want something like SmartFoxServer.time (same on all clients). The idea is, this can be server time which is synced across all clients.

Did you get what i am trying to do? Is there something like this already present in smartfox. Do you send server timestamp with request header (Didn't find in docs).

This can really be useful in many scenarios and reduces the need of schedulers for most of the things.



I implemented a solution in my games which works smoothly. I created a 250ms game tick on the serverside java extension, then I use Flash's timer functions to create a 250ms gameTick in the client. When you enter a game, the client requests the current gameTick for the room which is then set for the client... So both run in sync.

Code: Select all

public class gameTickHandler extends BaseClientRequestHandler {
    @Override
    public void handleClientRequest(User Sender, ISFSObject Params) {
        Params.putInt("id",Sender.getId());
        getParentExtension().handleInternalMessage(extensionConstants.GET_GAME_TICK, Params);


I personally do not use server time. I use one serverside clock for each game extension. All my game rooms/mmo rooms are running on their own clocks which start at 0 when the room is created.

Code: Select all

else if (cmdName == extensionConstants.GET_GAME_TICK) {
            User targetUser = sfsApi.getUserById(newParams.getInt("id"));
            newParams.putByte("t",(byte) 50);
            newParams.putInt("gt",gameTick);
            List<User> targetUsers = new LinkedList<User>();
            targetUsers.add(targetUser);
            sfsApi.sendObjectMessage(mapRoom,targetUser,newParams,targetUsers);



If you want precision high accuracy time synchronization, you will need to activate the clientside lag monitor to calculate latency to server. Then when your client requests time from the server, you can calculate in the latency.

If you want super accurate say <20ms accuracy server to client time synchronization... You will need to build an algorithm to get the time from the server each lag check then combine say... The last 10 values to estimate with high precision the exact time on the server and smooth out the latency fluctuation. The formula is simple... To smooth two time values received in the client from server: ((Interval Between Time Samples + Last Sample) + CurrentSample)/2; And, if you want higher accuracy... Add together ten or twenty samples, and divide by how many!

Should help you get the idea...

Code: Select all

sfs.addEventListener(SFSEvent.PING_PONG, onLagCheck);
        //Enable ten second lag check
        sfs.enableLagMonitor(true, 10);


    private function onLagCheck(e:SFSEvent):void {
        Lag = e.params.lagValue; //Private var
       
        //Send request to server to check server time here, then calculate in the last latency measurement.
        getGameTick();   
    }
   
        public function getGameTick():void {
      var sfsDataObj:ISFSObject = SFSObject.newInstance();
      sfsDataObj.putInt("lag", Lag);
      sfs.send(new ExtensionRequest(GET_GAME_TICK, sfsDataObj, currentRoom));
    }

//And, back to the client...

    private function onExtensionResponse(e:SFSEvent):void {
        var Response:String = e.params.cmd;
        var Data:ISFSObject = e.params.params as ISFSObject;

        if (Response == "gameTick") {
              gameTick = Data.getInt("gameTick");//Game tick with last lag estimate already calculated in.

Last edited by meanvel on 21 Apr 2017, 07:50, edited 2 times in total.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Server time on Client or server client time synchronisation

Postby Lapo » 21 Apr 2017, 07:42

Avinash wrote:Did you get what i am trying to do? Is there something like this already present in smartfox. Do you send server timestamp with request header (Didn't find in docs).

Yes I get what you mean, and no we don't provide this feature out of the box.

This can really be useful in many scenarios and reduces the need of schedulers for most of the things.

It's also more expensive than using a Scheduler because it needs constant lag measurement to keep everyone in sync, which in turn uses more bandwidth.

This is not bad per se, but in many cases it's just not necessary. When it is, of course, there's no substitute for it. We use a similar technique in a couple of our real-time examples.

I have a added a note to our todo list for future expansions.

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
meanvel
Posts: 129
Joined: 19 Jan 2017, 14:06

Re: Server time on Client or server client time synchronisation

Postby meanvel » 21 Apr 2017, 08:05

The second approach of using multiple samples to create very high accuracy time synchronization from clients to server is only necessary if your game has events occurring which you want to happen precisely at the same time in every client. (Synced within a few frames of each other.)

Say for example... A giant ball which falls from the ceiling in a multiplayer puzzle/platformer at a precise interval which crushes players under it.

For simple turn based games... High <20ms accuracy synchronization of time across clients is unnecessary and very costly. The simple approach I use of getting the server tick ONCE for the clients to sync their local tick is more accurate than 100ms for most players.
User avatar
Avinash
Posts: 3
Joined: 20 Apr 2017, 10:46

Re: Server time on Client or server client time synchronisation

Postby Avinash » 21 Apr 2017, 08:57

Thanks meanvel,

Thats exactly what i needed. Since its a card game, +- 250ms is fine. I liked the approach of calculating accurate time sync. Will post my solution here so that it can be of use for someone.

Thanks Lapo. Actually it wont consume any bandwidth (or pretty less). I will just sync my time with all (or important) requests from server. So i don't need to create a separate time sync request.

My Use case scenarios:
I have implemented 4 player multiplayer race (Ninjump dash). Game starts in few steps (All steps synced with time). First server sends time to end wait time in lobby. Once its over, server sends race start time to all clients. everyone shows count down for whatever time is left after receiving that call from server. the race starts at exact moment on all clients.

There are many situations like this where you want to sync action on all clients. And one common time (or tick) is the solution for this.

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 36 guests