-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from mladion/add_signalR_using_webSockets
Add signalR using web sockets
- Loading branch information
Showing
13 changed files
with
202 additions
and
29 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,22 @@ | ||
@page "/" | ||
@using Microsoft.AspNetCore.SignalR.Client; | ||
@inject HttpClient Http | ||
|
||
<div id="app"> | ||
<Chessboard /> | ||
</div> | ||
@if (@inGame) | ||
{ | ||
<div id="app"> | ||
<Chessboard HubConnection="@hubConnection" TableId="@tableId" IsWhitePlayer="@isWhitePlayer"/> | ||
</div> | ||
} | ||
else | ||
{ | ||
<button @onclick="CreateGame">Create Game</button> | ||
|
||
@if (tables != null) | ||
{ | ||
@foreach (string tableId in tables) | ||
{ | ||
<button @onclick="() => JoinGame(tableId)">Join Game @tableId</button> | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System.Net.Http.Json; | ||
using Microsoft.AspNetCore.SignalR.Client; | ||
|
||
namespace Client.Pages | ||
{ | ||
public partial class Index | ||
{ | ||
HubConnection hubConnection = new HubConnectionBuilder().WithUrl("https://localhost:7197/connect").Build(); | ||
|
||
bool inGame = false; | ||
bool isWhitePlayer = true; | ||
string tableId = ""; | ||
|
||
List<string>? tables = new List<string>(); | ||
|
||
protected override async Task OnInitializedAsync() | ||
{ | ||
await RefreshTables(); | ||
} | ||
|
||
public async Task RefreshTables() | ||
{ | ||
tables = await Http.GetFromJsonAsync<List<string>>("/table/getTables"); | ||
} | ||
|
||
public async Task CreateGame() | ||
{ | ||
await hubConnection.StartAsync(); | ||
|
||
tableId = Guid.NewGuid().ToString(); | ||
await hubConnection.SendAsync("JoinTable", tableId); | ||
inGame = true; | ||
} | ||
|
||
public async Task JoinGame(string gameId) | ||
{ | ||
await hubConnection.StartAsync(); | ||
this.tableId = gameId; | ||
isWhitePlayer = false; | ||
await hubConnection.SendAsync("JoinTable", gameId); | ||
inGame = true; | ||
} | ||
} | ||
} | ||
|
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Shared.Data | ||
{ | ||
public class TableManager | ||
{ | ||
public Dictionary<string, int> Tables { get; set; } = new(); | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Shared.Data; | ||
|
||
namespace WebAPI.Controllers | ||
{ | ||
[Route("table")] | ||
[ApiController] | ||
public class TableController : ControllerBase | ||
{ | ||
private readonly TableManager _tableManager; | ||
|
||
public TableController(TableManager tableManager) | ||
{ | ||
_tableManager = tableManager; | ||
} | ||
|
||
[HttpGet("getTables")] | ||
public IEnumerable<string> GetTables() | ||
{ | ||
return _tableManager.Tables.Where(x => x.Value < 2).Select(x => x.Key); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Microsoft.AspNetCore.SignalR; | ||
using Shared.Data; | ||
|
||
namespace WebAPI.Hubs | ||
{ | ||
public class MultiplayerHub : Hub | ||
{ | ||
private readonly TableManager _tableManager; | ||
|
||
public MultiplayerHub(TableManager tableManager) | ||
{ | ||
_tableManager = tableManager; | ||
} | ||
|
||
public async Task JoinTable(string tableId) | ||
{ | ||
if (_tableManager.Tables.ContainsKey(tableId)) | ||
{ | ||
if (_tableManager.Tables[tableId] < 2) | ||
{ | ||
await Groups.AddToGroupAsync(Context.ConnectionId, tableId); | ||
|
||
await Clients.GroupExcept(tableId, Context.ConnectionId).SendAsync("TableJoined"); | ||
|
||
_tableManager.Tables[tableId]++; | ||
} | ||
} | ||
else | ||
{ | ||
await Groups.AddToGroupAsync(Context.ConnectionId, tableId); | ||
_tableManager.Tables.Add(tableId, 1); | ||
} | ||
} | ||
|
||
public async Task Move(string tableId, int previousRow, int previousColumn, int newRow, int newColumn) | ||
{ | ||
await Clients.GroupExcept(tableId, Context.ConnectionId).SendAsync("Move", previousRow, previousColumn, newRow, newColumn); | ||
} | ||
} | ||
} |
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
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