Nice article. It inspired me to make an app using signalr. My app works well on localhost, but when I deploy on Azure I got: forbidden by its access permissions. ---> System.Net.Sockets.SocketException (10013): An attempt was made to access a socket in a way forbidden by its access permissions. at System.Net.Http.ConnectHelper.ConnectAsync. Anyone able to deploy blazor serverside app with signalr?
The Demo video link is missed or removed during article review. Please look at the below youtube video for the demo. https://www.youtube.com/watch?v=ZNaKNU11r-4
In this article, we will see how to create a bot vs. multiplayer tic-tac-toe game in Blazor. Blazor is an open-source .NET web front-end framework that allows us to create client-side applications using C# and HTML. This is a simple ASP.NET Core hosted server-side Blazor front-end application with Game UI Razor component and SignalR game hub to connect players with the bot to play the game. The Game Bot is created with .NET Core Background Service with core game engine to identify the best move against a player using a minimax recursive algorithm. The entire source code is uploaded in my GitHub repository.
Architecture
Tic-Tac-Toe Blazor App
This is a server-side Blazor App with Tic-Tac-Toe UI razor component. The razor component will have the game board design and its logic.
SignalR Game Hub This hub holds the SignalR methods to send messages between player and bot.
Tic-Tac-Toe Bot Client Bot client is based on .NET Core background service and contains the core game engine using the minimax algorithm. Whenever the player sends the move details to SignalR hub, it will send it to bot with the current board state and bot will get the next best spot available using core game engine and send it back to hub with the bot move. The hub will send back to the caller, and the player UI will get updated the move details in real-time.
How It Works - Demo
Steps
As a first step, launch the latest Visual Studio 2019 and create a new Blazor project by selecting ASP.NET Core web application and select the Blazor Server App.
I used the Blazor Server-Side app for this example but you can use client-side Blazor as well. Right now, the client-side Blazor app doesn’t have any official Blazor SignalR client due to the dependency of web socket support in the runtime. However, there are community versions of the Blazor SignalR client available.
In the Solution Explorer, add a new Razor component called TicTacToe.razor file and put the Tic-Tac-Toe board design and logic in the component. It also initializes the SignalR hub client.
In this component, we have three layouts. The main layout will render the tic-tac-toe board. The other two layouts will show the result of the winner or draw panel. The main layout is using the bootstrap container to design the board, and each cell is associated with onclick event method to notify the hub with the selected cell value.
In the OnInitAsync method, we initialize the board with the default index values. By default, the player will use X symbol and the bot will use O symbol to play.
We will also initialize the SignalR hub in OnInitAsync method. On click of the cell, the OnSelect method gets executed and the board item array will now have the data with the player move and send the entire board array as a parameter to hub method OnUserMoveReceived. It also listens to NotifyUser Hub method which is invoked by a bot with its move.
This hub class will hold the following signalR methods.
OnBotConnected This method gets executed when the bot is connected to the SignalR hub. It also adds the bot client into BOT group. This group is used to communicate with BOT only to send the message with the latest move from the player.
OnBotDisconnected This method gets executed when the bot is disconnected from SignalR hub. It also removes the bot from BOT group.
OnBotMoveReceived This method is used to notify the player (caller) after bot finish with the move and ready for the player to respond.
OnUserMoveReceived This method is used to notify the bot after the player finishes with the move and ready for a bot to respond.
The game bot is developed using .Net Core background service; when it started, it will connect to SignalR hub. When it joins, it invokes OnBotConnected method to add it into the BOT SignalR group. When it receives the message from the hub with the board array data, it calculates the next best move by calling GetBestSpot method from the game engine and sends it back to the caller with its move.
When the background service is stopped, it disposes the SignalR connection and removes it from the BOT group.
Core Game Engine
publicclass GameEngine
{
publicreadonlystring botPlayer = "O";
publicreadonlystring humanPlayer = "X";
public Move GetBestSpot(string[] board, string player)
{
Move bestMove = null;
var availableSpots = GetAvailableSpots(board);
foreach (var spot in availableSpots)
{
string[] newboard = (string[])board.Clone();
var newMove = new Move();
newMove.index = spot;
newboard[int.Parse(spot)] = player;
if (!IsWon(newboard, player) && GetAvailableSpots(newboard).Length > 0)
(board[0] == player && board[1] == player && board[2] == player) ||
(board[3] == player && board[4] == player && board[5] == player) ||
(board[6] == player && board[7] == player && board[8] == player) ||
(board[0] == player && board[3] == player && board[6] == player) ||
(board[1] == player && board[4] == player && board[7] == player) ||
(board[2] == player && board[5] == player && board[8] == player) ||
(board[0] == player && board[4] == player && board[8] == player) ||
(board[2] == player && board[4] == player && board[6] == player)
)
{
returntrue;
}
else
{
returnfalse;
}
}
publicbool IsPlayed(string input)
{
return input == "X" || input == "O";
}
}
publicclass Move
{
publicint score;
publicstring index;
}
I used the minimax algorithm in the game engine to find the best available spot. Minimax algorithm is a recursive algorithm which will play all possible movement by itself and as an opponent, until it reaches the terminal state (win or draw) and then decides the best move from all possible iterations. You can refer to this article to understand more details about the minimax algorithm.
Conclusion
Blazor is super useful for .NET developers who are not interested in learning javascript for front-end development. This article shows how easy it is to develop a real-time blazor application with SignalR. I have used the minimax algorithm to identify the best spot available. It will be more interesting to use reinforcement machine learning algorithms for AI to learn and identify based on rewards instead of recursive minimax algorithms. This will be a good use case to try when ML.NET introduce reinforcement learning library.
The entire source code is uploaded in my GitHub repository. Happy Coding!!!
Jason ChenPosted Sep 30, 2019, 12:15 PM
Nice article. It inspired me to make an app using signalr. My app works well on localhost, but when I deploy on Azure I got: forbidden by its access permissions. ---> System.Net.Sockets.SocketException (10013): An attempt was made to access a socket in a way forbidden by its access permissions. at System.Net.Http.ConnectHelper.ConnectAsync. Anyone able to deploy blazor serverside app with signalr?
Mathias JensenPosted Aug 5, 2019, 10:59 AM
When I start debug I get the error that the connection was actively refused?
Prabu ElavarasanPosted Jul 17, 2019, 9:49 PM
Nice article. Keep going :-)
Jeeva SubburajPosted Jul 17, 2019, 9:25 AM
The Demo video link is missed or removed during article review. Please look at the below youtube video for the demo. https://www.youtube.com/watch?v=ZNaKNU11r-4