Skip to content

Instantly share code, notes, and snippets.

View Ibro's full-sized avatar

Ibrahim Šuta Ibro

View GitHub Profile
Ibro / ChatHub.cs
Created December 27, 2022 00:43
SignalR ChatHub to talk to the clients
View ChatHub.cs
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
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>();
View Startup.cs
services.AddScoped<IDbInitializer, DbInitializer>();
View Program.cs
public static void Main(string[] args)
View DbInitializer.cs
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
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
app.UseEndpoints(endpoints = >
endpoints.MapHub < Connect4Hub > ("/connect4");
View GameBoard.cs
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
public class GamePiece
public PieceColor Color;
public GamePiece() => Color = PieceColor.Blank;
public GamePiece(PieceColor color) => Color = color;
View PieceColor.cs
public enum PieceColor