I am developing the server for a browser game, it uses an ASP.NET API for authentication, login and character creation and a websocket for the rest of the game (Microsoft.Web.WebSockets).

While on combat mode, each character will have a fixed amount of time to carry out their actions.

is there a way to keep track of the time on the server or should I create a different server to serve as time master ?

Which method/library should I use to control the time ?


Your server can keep time.

The first thing you will need is to have the server start a turn. You would then notify the client that his turn has started and how much time they have to complete their move (this can be used to present a countdown in UI).

Afterwards, one of the following will happen:

  • The time is over, the server must be able to advance to the next turn even if the client has given no input or if the client has disconnected.

  • The client sends the action back to the server, the server needs to check how much time has passed, accept or reject the move, and advance to the next turn.

A simple approach would then be to have a permanent object for each game session; this object will have a turn timer. Then you are creating a race condition between the timer and the client response...

I'd suggest to use the Reactive Extensions (MSDN) (GitHub) (NuGet). You can create an IObservable※ that calls its subscribers when the client sends a response (you can make one for each client, you can even use it to represent disconnection※※). Then you can use Observable.TimeOut to create an IObservable for the turn that will wait for a response from the client IObservable or a timeout. This way, the race condition is responsibility of Observable.TimeOut.

※: It is actually IObservable<T> where T is a class that represents the action of the client.

※※: You can call OnNext when you receive input from the client, and OnCompleted when the client disconnects.

The next step is to react to the IObservable from Observable.TimeOut. You will either get:

  • A call to OnNext with an object that represents the action of the client.
  • A call to OnError with a TimeoutException, which tells you that the client took too long to response.
  • A call to OnCompleted that tells you that the client has disconnected.

You would then proceed to notify the client if whatever or not their input was in time (if the client is still connected, that is). Pick the client for the next turn, start listening to the appropriate IObserver and notify that client that their turn has started.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .