Skip to content

Tic Tac Toe

Adrian Reithaug edited this page Jan 3, 2019 · 1 revision

Tic Tac Toe

Some features of this version of the game:

  • Assign a player ID to yourself
  • Join any game room, either as a player or as a spectator
  • If a player leaves, and you're a spectator, you can immediately partake in the game
  • Keep track of your score across the game rooms

Implementation

While tic tac toe is an unoriginal idea at this point (that is an understatement), using ASP.NET Core SignalR for real-time multiplayer (using either WebSockets, Server-Sent Events, or Long Polling) makes it less so.

SignalR is a fantastic open-source library integrated in ASP.NET Core, which uses hubs to communicate between clients and servers.

In short, the process boils down to this:

  1. A client enters the tic tac toe page, and is immediately assigned a context with a connection ID.
  2. Using this context, the server can keep track of to whom it should send certain responses to.
  3. Whenever the client performs an action which results in the hub being called (e.g. making a move, or simply joining a game), some JavaScript function invokes a method on the hub (i.e. the server), which asynchronously sends the requested data back to the client (in real-time). Here, some more JavaScript manipulates the DOM with the server's response.

Thus, not a single piece of data is stored in a database, the game logic and player/game tracking is happening on the server, and the clients interact with each other without even the slightest interruption.

Best of all? If you have an existing code base, it does not take a lot of modifications to make your application a real-time application.

Clone this wiki locally