Created
May 24, 2021 09:17
-
-
Save devmobasa/5dc058ca88060403f97e9d37a338252d to your computer and use it in GitHub Desktop.
https://codingblast.com/connect4-live-game-signalr-blazor/ - SignalR Connect4 Blazor
This file contains hidden or 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"; | |
public Connect4Hub(ILogger < Connect4Hub > logger) { | |
_logger = logger; | |
} | |
public async Task JoinedQueue() { | |
await Groups.AddToGroupAsync(Context.ConnectionId, Connect4Group); | |
} | |
public async Task GameReady(GameBoard board) { | |
await Clients.Group(Connect4Group).SendAsync(OnGameReady, board); | |
} | |
public async Task OnPlayerDisconnected(Guid userId) { | |
await Groups.RemoveFromGroupAsync(Context.ConnectionId, Connect4Group); | |
_logger.LogWarning("User left"); | |
await Clients.Group(Connect4Group).SendAsync(NotifyOnPlayerLeft, userId); | |
} | |
public async Task OnOpponentMoveReceived(GameBoard board) { | |
await Clients.Group(Connect4Group).SendAsync(NotifyGameHost, board); | |
} | |
public async Task OnHostMoveReceived(GameBoard board) { | |
await Clients.Group(Connect4Group).SendAsync(NotifySecondPlayer, board); | |
} | |
public async Task OnGameOver(GameBoard board, WinningPlay winningPlay) { | |
await Clients.Group(Connect4Group).SendAsync(NotifyOnGameOver, board, winningPlay); | |
} | |
public async Task OnDraw(GameBoard board) { | |
await Clients.Group(Connect4Group).SendAsync(NotifyOnDraw, board); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment