2
\$\begingroup\$

I am making a game server that has 60 Update-Per-Second (assume I can most of the time achieve it). In the current model, my main loop does the following tasks in sequence:

  • Update game logic (position, etc...)
  • Serialize the data to JSON string
  • Broadcast the data (through WebSocket) to the clients. Currently the broadcast method is async, that means my server does not stop and wait for the broadcast to finish.

I have 3 questions:

  • Is it good to send the packages at this frequency?
  • Should I cap the UPS to 30 instead?
  • Should I separate the broadcast into another thread, so no matter the UPS of my server, the broadcast will do asynchronous time and depend on the update?

Here is my current code:

public MusicGame(GameRoom room)
{
    this.Room = room;
    this.ConnectedSessions = new List<GameBehavior>();

    this.timer = new Stopwatch();
    this.timer.Start();

    new Thread(() =>
    {
        while (!this.Disposed)
        {
            float delta = (float)this.timer.Elapsed.TotalSeconds;
            if (this.timer.Elapsed.TotalSeconds < MusicGame.FpsLimit)
            {
                Thread.Sleep((int)((MusicGame.FpsLimit - delta) * 1000));
                continue;
            }
            this.timer.Restart();

            delta = Math.Min(DeltaTimeLimit, delta);
            this.Act(delta);
        }
    }).Start();

    this.CurrentScreen = new WaitingScreen(this);
}

public void Act(float delta)
{
    if (this.CurrentScreen != null)
    {
        this.CurrentScreen.Act(delta);
        this.CurrentScreen.SerializeData();

        var data = this.CurrentScreen.SerializedData;
        this.BroadcastGameAsync(data);
    }
}

I am using C# Console Application and WebSocketSharp if it matters.

\$\endgroup\$
2
\$\begingroup\$

As for the update frequency it depends from the type of game. For an action type of game 30 updates per seconds should be perfectly smooth, but for an mmorpg (where you have thousands of players) better use something like 10 per second, to save server time.

I doubt there will be any reason to have 60 updates per second, that will just use a lot of server time and I don't think anything in the game will change that fast.
If you do want to send an update right after that happens, you can just call the SendUpdate() function.

Since question was edited I'll remove this part:

Generally you should separate the frame update from the networking updates, as those 2 are not depending on each other.
Also you should use vsync to limit the frame rate.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ What is the vsync you are mentioning? My server does not do the rendering work, so I don't know if I can really apply vsync here? \$\endgroup\$ – Luke Vo Jun 15 '15 at 19:21
  • \$\begingroup\$ @DatVM Vsync is the refresh rate of the monitoy, most games use that. But wait, why are you even mentioning FPS if you are asking about the server. Did you mean updates per second (UPS) ? \$\endgroup\$ – zoran404 Jun 16 '15 at 12:40
  • \$\begingroup\$ Oh my bad, that was what I meant. Thank you for pointing out. \$\endgroup\$ – Luke Vo Jun 16 '15 at 22:31
  • \$\begingroup\$ Ive heard the advice that the network update rate should be no larger than what is needed for smooth gameplay \$\endgroup\$ – Waterlimon Jun 20 '15 at 16:36
  • \$\begingroup\$ @Waterlimon You are right. The lower the update rate, the more clients a single server can support. \$\endgroup\$ – zoran404 Jun 20 '15 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.