服务器和客户端时序不同

问题描述 投票:0回答:1

我有一个客户端服务器的情况,每一方都在测量时间,只是似乎存在一个问题,即测量的时间不匹配。长话短说,这个想法是有一个倒计时,之后程序需要做的事情。我在服务器端测量它。但是,需要显示倒计时,所以我所做的就是在客户端单独运行它。最终结果是,当服务器发送消息时,客户端显示23秒,该消息指示倒计时10分钟的时间。

客户端是XNA,代码:

MillisecCount += gameTime.ElapsedGameTime.Milliseconds;
if (MillisecCount >= 1000)
    {
    MillisecCount -= 1000;
    Timer++;
    }

然后从可用时间中减去计时器并显示。在服务器端,这种情况正在发生:

async Task timeOut(int delay, CancellationToken ct)
    {
    await Task.Delay(1000 * delay);

    ct.ThrowIfCancellationRequested();
    }

void sendTimeOutMessage(Task t)
    {
    //Send timeout message on network.
    }

void reTime()
    {
    CancellationTokenSource cts = new CancellationTokenSource();
    CancelStack.Push(cts);
    Task t = timeOut(maxTime - Timer, cts.Token);
    t.ContinueWith(sendTimeOutMessage, TaskContinuationOptions.OnlyOnRanToCompletion);
    }

在具有23秒差异的测试场景中,reTime()仅在倒计时开始时被调用一次。

time timer server xna client
1个回答
1
投票

好的,事实证明有时XNA不计算时间,所以这使客户端有点慢。在客户端使用秒表同步双方。

© www.soinside.com 2019 - 2024. All rights reserved.