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.
This commit is contained in:
59
Shogi.UI/Shared/GameHubNode.cs
Normal file
59
Shogi.UI/Shared/GameHubNode.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user