View ChatHub.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.SignalR; | |
namespace BlazorSignalRChat; | |
// We're creating a Chat Hub - Hub is a SignalR class | |
// This class will be used to send messages to the client | |
// and receive messages from the client | |
public class ChatHub : Hub | |
{ | |
// We will create a constant to be used from the client (Blazor page) |
View HostSeedExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static IHost SeedIfNeeded(this IHost host) { | |
using var scope = host.Services.CreateScope(); | |
var services = scope.ServiceProvider; | |
var logger = services.GetRequiredService<ILogger<IDbInitializer>>(); | |
try { | |
logger.LogInformation("Checking if Database is created and if seed needs to be done."); | |
var dbInitializer = services.GetRequiredService<IDbInitializer>(); | |
dbInitializer.Seed().GetAwaiter().GetResult(); |
View Startup.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services.AddScoped<IDbInitializer, DbInitializer>(); |
View Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void Main(string[] args) | |
{ | |
CreateHostBuilder(args) | |
.Build() | |
.SeedIfNeeded() | |
.Run(); | |
} |
View DbInitializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IDbInitializer { | |
Task Seed(); | |
} | |
public class DbInitializer: IDbInitializer { | |
private readonly ILogger < DbInitializer > _logger; | |
private readonly UserManager < IdentityUser > _userManager; | |
private readonly ApplicationDbContext _dbContext; | |
private readonly IWebHostEnvironment _environment; |
View Connect4Hub.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Connect4Hub: Hub { | |
private readonly ILogger < Connect4Hub > _logger; | |
public const string OnGameReady = "OnGameReady"; | |
public const string NotifySecondPlayer = "NotifySecondPlayer"; | |
public const string NotifyGameHost = "NotifyGameHost"; | |
public const string NotifyOnGameOver = "NotifyOnGameOver"; | |
public const string NotifyOnDraw = "NotifyOnDraw"; | |
public const string NotifyOnPlayerLeft = "NotifyOnPlayerLeft"; | |
private const string Connect4Group = "Connect4"; |
View Startup.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.UseEndpoints(endpoints = > | |
{ | |
endpoints.MapControllers(); | |
endpoints.MapBlazorHub(); | |
endpoints.MapHub < Connect4Hub > ("/connect4"); | |
endpoints.MapFallbackToPage("/_Host"); | |
}); |
View GameBoard.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GameBoard | |
{ | |
public GamePiece[, ] Board { get; set; } | |
public GameBoard() | |
{ | |
Board = new GamePiece[7, 6]; | |
// Populate the Board with blank pieces | |
for (int i = 0; i <= 6; i++) { |
View GamePiece.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GamePiece | |
{ | |
public PieceColor Color; | |
public GamePiece() => Color = PieceColor.Blank; | |
public GamePiece(PieceColor color) => Color = color; | |
} |
View PieceColor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum PieceColor | |
{ | |
Red, | |
Yellow, | |
Blank | |
} |
NewerOlder