【问题标题】:C# (Unity) Create Synced Network TimeC# (Unity) 创建同步网络时间
【发布时间】:2016-10-20 15:20:48
【问题描述】:

我一直在讨论如何最终通过 60 个选定点同步 20 个对象的运动。我有几个选择,主要是

  • 使用同步变量在点(1 到 0)之间同步它们的进度,但这可能会出现滞后且相当密集
  • 使用 unity 的内置网络转换,但这在网络能力方面似乎代价高昂,因为其他设备只需要知道点和何时开始
  • 首先,发送我已成功完成的点列表,然后使设备在某个时间以固定速率开始移动

这就是我得出的结论,即我需要一个精确到 0.1 秒左右的联网通用时间,这样用户才能看到相同的运动。如果您认为这是这种网络的错误方法,请告诉我,因为我是网络的初学者。

到目前为止,我已经尝试了三种方法来获得这个同步时间。

  1. 我使用 System.DateTime 认为这是一个准确的通用时间,但发现设备之间的差异超过一秒

  2. 我试图计算两个设备之间的延迟,以便我可以计算和删除设备时间变化,通过

    • (A) 使用 GetAveragePing(NetworkPlayer player); 的内置方法,但由于 NetworkPlayer 类似乎不再具有功能和兼容性,因此该方法已过时且已被贬低
    • (B) 使用 GetCurrentRtt(int hostId, int connectionId, out byte error); 的另一种内置方法,它返回 0,我相信可能会再次贬值
    • (C) 通过从服务器向客户端发送消息,然后将所用时间除以 2,但这似乎不准确,因为我试图计算服务器和客户端之间的延迟,这与客户端之间的延迟不同和服务器,所以不完全是一半
  3. 我尝试通过

    从服务器访问某种形式的同步网络时间
    • (A) 使用here 中的代码获取网络时间,然后计算出某个点的网络日期时间之间的 System.DateTime 差异,以便我可以同步它们。我用来执行此操作的脚本如下。
    • (B) 从GetNetworkTimestamp(); 获取网络时间戳,我认为这可能是我想要的

所以对于所有这些方法都失败了,所以今天我问你;

  • 还有其他同步时间的方法吗?

  • 这些方法中的一种是否可行,我是否出错了,在这种情况下,我可以提供有关我的问题和使用的代码的更多详细信息吗?

  • 我的网络方法完全错误,或者至少大部分错误,您建议我如何实现我的预期目标?

非常感谢您阅读本文,我希望它详细且仍然清晰,如果我可以帮助您加深对我的问题的理解,我将非常乐意提供帮助。感谢您提供任何答案/启发 cmets。

网络时间代码

脚本 A(获取网络时间)

using UnityEngine;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using UnityEngine.UI;
public class GetNetworkTime : MonoBehaviour {

    public static System.DateTime NetworkTime()
    {
        //default Windows time server
        const string ntpServer = "time.windows.com";

        // NTP message size - 16 bytes of the digest (RFC 2030)
        var ntpData = new byte[48];

        //Setting the Leap Indicator, Version Number and Mode values
        ntpData[0] = 0x1B; //LI = 0 (no warning), VN = 3 (IPv4 only), Mode = 3 (Client Mode)

        var addresses = Dns.GetHostEntry(ntpServer).AddressList;

        //The UDP port number assigned to NTP is 123
        var ipEndPoint = new IPEndPoint(addresses[0], 123);
        //NTP uses UDP
        var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

        socket.Connect(ipEndPoint);

        //Stops code hang if NTP is blocked
        socket.ReceiveTimeout = 3000;

        socket.Send(ntpData);
        socket.Receive(ntpData);
        socket.Close();

        //Offset to get to the "Transmit Timestamp" field (time at which the reply 
        //departed the server for the client, in 64-bit timestamp format."
        const byte serverReplyTime = 40;

        //Get the seconds part
        ulong intPart = System.BitConverter.ToUInt32(ntpData, serverReplyTime);

        //Get the seconds fraction
        ulong fractPart = System.BitConverter.ToUInt32(ntpData, serverReplyTime + 4);

        //Convert From big-endian to little-endian
        intPart = SwapEndianness(intPart);
        fractPart = SwapEndianness(fractPart);

        var milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000L);

        //**UTC** time
        var networkDateTime = (new System.DateTime(1900, 1, 1, 0, 0, 0, System.DateTimeKind.Utc)).AddMilliseconds((long)milliseconds);

        //return networkDateTime.ToLocalTime();
        return networkDateTime;
    }

    // stackoverflow.com/a/3294698/162671
    static uint SwapEndianness(ulong x)
    {
        return (uint)(((x & 0x000000ff) << 24) +
                       ((x & 0x0000ff00) << 8) +
                       ((x & 0x00ff0000) >> 8) +
                       ((x & 0xff000000) >> 24));
    }
}

脚本 B(差值计算器和时间记录器)

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using UnityEngine.UI;

    public class SyncTime2 : NetworkBehaviour {

        public float serverTimeDif;
        public float clientTimeDif;

        GetNetworkTime GetNetworkTime;

        GameObject time;
        Text TimeLog;

        // Use this for initialization
        void Start () {
            GetNetworkTime = GetComponent<GetNetworkTime>();
            time = GameObject.Find("time");
            TimeLog = time.GetComponent<Text>();
            if (isServer)
            {
                serverTimeDif = (float)System.DateTime.UtcNow.TimeOfDay.TotalSeconds - (float) GetNetworkTime.NetworkTime().TimeOfDay.TotalSeconds;
                StartCoroutine("DisplayTime", serverTimeDif);
            }
            else
            {
                clientTimeDif = (float)System.DateTime.UtcNow.TimeOfDay.TotalSeconds - (float)GetNetworkTime.NetworkTime().TimeOfDay.TotalSeconds;
                StartCoroutine("DisplayTime", clientTimeDif);
            }
        }

        IEnumerator DisplayTime (float TimeDif)
        {
            for (;;)
            {
                TimeLog.text = "" + ((float)System.DateTime.UtcNow.TimeOfDay.TotalSeconds + TimeDif);
                // Log time so i can see if it is different on different devices
                yield return new WaitForSeconds(0.01f);
            }
        }
    }

【问题讨论】:

    标签: c# networking unity3d time synchronization


    【解决方案1】:

    Hmmmff...似乎只是添加时间差而不是减去它可以工作并同步时间,而不是

    TimeLog.text = "" + ((float)System.DateTime.UtcNow.TimeOfDay.TotalSeconds - TimeDif);

    我应该使用

    TimeLog.text = "" - ((float)System.DateTime.UtcNow.TimeOfDay.TotalSeconds + TimeDif);

    如果有人可以确认这是/不是正确的联网方法,我会认为这是一个答案

    【讨论】:

      猜你喜欢
      • 2018-08-16
      • 2015-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-06
      相关资源
      最近更新 更多