diff --git a/Shogi.Api/Controllers/SessionsController.cs b/Shogi.Api/Controllers/SessionsController.cs index 75614aa..9e72c63 100644 --- a/Shogi.Api/Controllers/SessionsController.cs +++ b/Shogi.Api/Controllers/SessionsController.cs @@ -118,7 +118,7 @@ public class SessionsController : ControllerBase session.AddPlayer2(User.GetShogiUserId()); await sessionRepository.SetPlayer2(name, User.GetShogiUserId()); - await communicationManager.BroadcastToAll(new SessionJoinedByPlayerSocketMessage()); + await communicationManager.BroadcastToAll(new SessionJoinedByPlayerSocketMessage(session.Name)); return this.Ok(); } return this.Conflict("This game already has two players."); diff --git a/Shogi.Contracts/Socket/SessionJoinedByPlayerSocketMessage.cs b/Shogi.Contracts/Socket/SessionJoinedByPlayerSocketMessage.cs index 70fc4bf..d6c8f53 100644 --- a/Shogi.Contracts/Socket/SessionJoinedByPlayerSocketMessage.cs +++ b/Shogi.Contracts/Socket/SessionJoinedByPlayerSocketMessage.cs @@ -1,9 +1,15 @@ using Shogi.Contracts.Types; -namespace Shogi.Contracts.Socket +namespace Shogi.Contracts.Socket; + +public class SessionJoinedByPlayerSocketMessage : ISocketResponse { - public class SessionJoinedByPlayerSocketMessage : ISocketResponse + public SocketAction Action => SocketAction.SessionJoined; + + public string SessionName { get; } + + public SessionJoinedByPlayerSocketMessage(string sessionName) { - public SocketAction Action => SocketAction.SessionJoined; + SessionName = sessionName; } } diff --git a/Shogi.UI/Pages/Home/GameBoard/GameBoard.razor b/Shogi.UI/Pages/Home/GameBoard/GameBoard.razor index def4991..d7e316e 100644 --- a/Shogi.UI/Pages/Home/GameBoard/GameBoard.razor +++ b/Shogi.UI/Pages/Home/GameBoard/GameBoard.razor @@ -13,7 +13,7 @@ } else if (isSpectating) { - + } else { @@ -32,16 +32,25 @@ else protected override void OnInitialized() { base.OnInitialized(); - ShogiSocket.OnPlayerMoved += OnPlayerMoved_RefetchSession; - ShogiSocket.OnSessionJoined += + ShogiSocket.OnPlayerMoved += OnPlayerMoved_FetchSession; + ShogiSocket.OnSessionJoined += OnSessionJoined_FetchSession; } protected override async Task OnParametersSetAsync() { - await RefetchSession(); + await FetchSession(); } - async Task RefetchSession() + Task OnSessionJoined_FetchSession(SessionJoinedByPlayerSocketMessage args) + { + if (args.SessionName == SessionName) + { + return FetchSession(); + } + return Task.CompletedTask; + } + + async Task FetchSession() { if (!string.IsNullOrWhiteSpace(SessionName)) { @@ -59,11 +68,11 @@ else } } - Task OnPlayerMoved_RefetchSession(PlayerHasMovedMessage args) + Task OnPlayerMoved_FetchSession(PlayerHasMovedMessage args) { if (args.SessionName == SessionName) { - return RefetchSession(); + return FetchSession(); } return Task.CompletedTask; } diff --git a/Shogi.UI/Pages/Home/GameBrowser.razor b/Shogi.UI/Pages/Home/GameBrowser.razor index 886d39e..8ccdaf6 100644 --- a/Shogi.UI/Pages/Home/GameBrowser.razor +++ b/Shogi.UI/Pages/Home/GameBrowser.razor @@ -1,5 +1,6 @@ @implements IDisposable; +@using Shogi.Contracts.Socket; @using Shogi.Contracts.Types; @using System.ComponentModel.DataAnnotations; @using System.Net; @@ -98,7 +99,7 @@ { base.OnInitialized(); ShogiSocket.OnSessionCreated += FetchSessions; - ShogiSocket.OnSessionJoined += FetchSessions; + ShogiSocket.OnSessionJoined += OnSessionJoined_FetchSessions; Account.LoginChangedEvent += LoginChangedEvent_FetchSessions; } @@ -109,6 +110,9 @@ activeSession = s; ActiveSessionChanged?.Invoke(s); } + + Task OnSessionJoined_FetchSessions(SessionJoinedByPlayerSocketMessage args) => FetchSessions(); + Task LoginChangedEvent_FetchSessions(LoginEventArgs args) { if (args.User != null) @@ -137,7 +141,7 @@ public void Dispose() { ShogiSocket.OnSessionCreated -= FetchSessions; - ShogiSocket.OnSessionJoined -= FetchSessions; + ShogiSocket.OnSessionJoined -= OnSessionJoined_FetchSessions; Account.LoginChangedEvent -= LoginChangedEvent_FetchSessions; } diff --git a/Shogi.UI/Shared/ShogiSocket.cs b/Shogi.UI/Shared/ShogiSocket.cs index ea01560..0913225 100644 --- a/Shogi.UI/Shared/ShogiSocket.cs +++ b/Shogi.UI/Shared/ShogiSocket.cs @@ -11,7 +11,7 @@ namespace Shogi.UI.Shared; public class ShogiSocket : IDisposable { public event AsyncEventHandler? OnSessionCreated; - public event AsyncEventHandler? OnSessionJoined; + public event AsyncEventHandler? OnSessionJoined; public event AsyncEventHandler? OnPlayerMoved; private readonly ClientWebSocket socket; @@ -76,7 +76,8 @@ public class ShogiSocket : IDisposable case SocketAction.SessionJoined: if (this.OnSessionJoined is not null) { - await this.OnSessionJoined(); + var args = JsonSerializer.Deserialize(memory[..result.Count], serializerOptions); + await this.OnSessionJoined(args!); } break; case SocketAction.PieceMoved: