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 isasync, 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.