Author Topic: Server Time Assistance  (Read 3109 times)

voncarp

  • Jr. Member
  • **
  • Thank You
  • -Given: 13
  • -Receive: 2
  • Posts: 91
    • View Profile
Server Time Assistance
« on: August 07, 2015, 02:36:17 PM »
I'm looking to have random daily/weekly events.  I am thinking I need to check the time on the server so its consistent for all players. 

Would you have any example usage for TnManager.servertime?  I am getting some massive numbers with TnManager.servertime. Would you suggest this be the starting point in implementing something like this?

Any assistance would be appreciated.  Thanks.

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Re: Server Time Assistance
« Reply #1 on: August 07, 2015, 05:09:50 PM »
servertime is Ticks / 10000.  use:  new DateTime(servertime * 10000)

voncarp

  • Jr. Member
  • **
  • Thank You
  • -Given: 13
  • -Receive: 2
  • Posts: 91
    • View Profile
Re: Server Time Assistance
« Reply #2 on: August 08, 2015, 02:27:24 PM »
Thanks.

  1. using UnityEngine;
  2. using System;
  3. using TNet;
  4.  
  5. public class MultiplayerEvents : TNBehaviour {
  6.  
  7.         public long currentServerTime;
  8.  
  9.         void Start() {
  10.                 currentServerTime = TNManager.serverTime * 10000;
  11.  
  12.                 TimeSpan onedayTicks = new TimeSpan (1,0,0,0);
  13.                 DateTime currentServerDate = new DateTime (currentServerTime);
  14.                 DateTime nextServerDate = new DateTime(currentServerTime + onedayTicks.Ticks );
  15.  
  16.                 print ("server date is " + currentServerDate);
  17.                 print ("next update date is " + nextServerDate);
  18.  
  19.         }
  20. }

This seems to display the results in the editor properly.  However, once I connect to a remote server the results still show the time on my local PC and not the remote server.  Would you have thoughts on this?

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Re: Server Time Assistance
« Reply #3 on: August 08, 2015, 03:44:44 PM »
if you are not connected TNManager.servertime will be your localtime.  Otherwise it is from the server.

Attached is a demo.



edit: Execute your code when 'OnNetworkConnect' fires and it should display correctly.
« Last Edit: August 08, 2015, 06:25:46 PM by devomage »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Server Time Assistance
« Reply #4 on: August 08, 2015, 11:51:21 PM »
When working with server time, don't use the local time at all. All timers should be based on the server time instead. Server time is in milliseconds. If you want to have some event happen in 30 seconds, send an RFC to others with the time like so:
  1. tno.Send("SetExpirationTime", Target.AllSaved, TNManager.serverTime + 30 * 1000);
  1. [RFC]
  2. void SetExpirationTime (long expTime)
  3. {
  4.     float seconds = (expTime - TNManager.serverTime) * 0.001;
  5.     Debug.Log("Expiration event in " + seconds);
  6. }
Since you are sending the actual server time in that RFC, you can make it persistent (Target.AllSaved) and it will work fine even if clients join late.

voncarp

  • Jr. Member
  • **
  • Thank You
  • -Given: 13
  • -Receive: 2
  • Posts: 91
    • View Profile
Re: Server Time Assistance
« Reply #5 on: August 09, 2015, 01:36:17 PM »
if you are not connected TNManager.servertime will be your localtime.  Otherwise it is from the server.

Attached is a demo.



edit: Execute your code when 'OnNetworkConnect' fires and it should display correctly.

This example is not working for me.  Nor is the one that I provided.  Even updating it with OnNetworkConnect.  At least it doesn't work on a Mac to a Windows 2008 remote server with a blank project with only NGUI and TNET.




As you can see, it is only displaying the local time even though the remote server time is three hours different.  Another note is the actual local time is 815, not 1815.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Server Time Assistance
« Reply #6 on: August 14, 2015, 11:09:25 AM »
Note what I posted. Server time is not something you should be using manually.