Files
Shogi/Shogi.UI/Shared/GameHubNode.cs
Lucas Morgan 51d234d871 Replace custom socket implementation with SignalR.
Replace MSAL and custom cookie auth with Microsoft.Identity.EntityFramework
Also some UI redesign to accommodate different login experience.
2024-08-25 03:46:44 +00:00

60 lines
1.4 KiB
C#

using Microsoft.AspNetCore.SignalR.Client;
using Shogi.UI.Identity;
using System.Diagnostics;
namespace Shogi.UI.Shared;
public class GameHubNode : IAsyncDisposable
{
private readonly HubConnection hubConnection;
public GameHubNode()
{
this.hubConnection = new HubConnectionBuilder()
.WithUrl(new Uri("https://localhost:5001/gamehub", UriKind.Absolute), options =>
{
options.HttpMessageHandlerFactory = handler => new CookieCredentialsMessageHandler { InnerHandler = handler };
options.SkipNegotiation = true;
options.Transports = Microsoft.AspNetCore.Http.Connections.HttpTransportType.WebSockets;
})
.Build();
}
public bool IsConnected => this.hubConnection.State == HubConnectionState.Connected;
public async Task BeginListen()
{
if (!this.IsConnected)
{
await this.hubConnection.StartAsync();
}
}
public async Task Subscribe(string sessionId)
{
await this.hubConnection.SendAsync("Subscribe", sessionId);
}
public async Task Unsubscribe(string sessionId)
{
await this.hubConnection.SendAsync("Unsubscribe", sessionId);
}
public IDisposable OnSessionJoined(Func<Task> func)
{
return this.hubConnection.On("SessionJoined", func);
}
public IDisposable OnPieceMoved(Func<Task> func)
{
return this.hubConnection.On("PieceMoved", func);
}
public ValueTask DisposeAsync()
{
GC.SuppressFinalize(this);
return this.hubConnection.DisposeAsync();
}
}