-
-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Boilerplate signalr implementation (#8557)
- Loading branch information
Showing
12 changed files
with
95 additions
and
74 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
72 changes: 72 additions & 0 deletions
72
...ilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Components/SignalRHandler.cs
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,72 @@ | ||
using Boilerplate.Shared.Dtos.Identity; | ||
using Microsoft.AspNetCore.SignalR.Client; | ||
|
||
namespace Boilerplate.Client.Core.Components; | ||
|
||
public partial class SignalRHandler : AppComponentBase | ||
{ | ||
[AutoInject] private MessageBoxService messageBoxService = default!; | ||
[AutoInject] private AuthenticationManager authManager = default!; | ||
|
||
private HubConnection? hubConnection; | ||
|
||
protected async override Task OnAfterFirstRenderAsync() | ||
{ | ||
await base.OnAfterFirstRenderAsync(); | ||
|
||
authManager.AuthenticationStateChanged += IsUserAuthenticated; | ||
|
||
await ConnectSignalR(); | ||
} | ||
|
||
private async void IsUserAuthenticated(Task<AuthenticationState> task) | ||
{ | ||
try | ||
{ | ||
await ConnectSignalR(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
ExceptionHandler.Handle(ex); | ||
} | ||
} | ||
|
||
private async Task ConnectSignalR() | ||
{ | ||
if (hubConnection is not null) | ||
{ | ||
await hubConnection.DisposeAsync(); | ||
} | ||
|
||
var access_token = await AuthTokenProvider.GetAccessTokenAsync(); | ||
|
||
hubConnection = new HubConnectionBuilder() | ||
.WithUrl($"{Configuration.GetServerAddress()}/identity-hub?access_token={access_token}") | ||
.Build(); | ||
|
||
hubConnection.On<string>("TwoFactorToken", async (token) => | ||
{ | ||
await messageBoxService.Show(Localizer[nameof(AppStrings.TwoFactorTokenPushText), token]); | ||
|
||
// The following code block is not required for Bit.BlazorUI components to perform UI changes. However, it may be necessary in other scenarios. | ||
/*await InvokeAsync(async () => | ||
{ | ||
StateHasChanged(); | ||
});*/ | ||
|
||
// You can also leverage IPubSubService to notify other components in the application. | ||
}); | ||
|
||
await hubConnection.StartAsync(CurrentCancellationToken); | ||
} | ||
|
||
protected override async ValueTask DisposeAsync(bool disposing) | ||
{ | ||
if (hubConnection is not null) | ||
{ | ||
await hubConnection.DisposeAsync(); | ||
} | ||
|
||
await base.DisposeAsync(disposing); | ||
} | ||
} |
6 changes: 5 additions & 1 deletion
6
src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Routes.razor
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
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
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