| | 1 | | using Microsoft.JSInterop; |
| | 2 | | using Microsoft.AspNetCore.Components; |
| | 3 | | using Microsoft.AspNetCore.SignalR.Client; |
| | 4 | |
|
| | 5 | | namespace Syki.Front.Auth; |
| | 6 | |
|
| 0 | 7 | | public class SignalRConnectionManager( |
| 0 | 8 | | IConfiguration configuration, |
| 0 | 9 | | ILocalStorageService storage, |
| 0 | 10 | | SykiAuthStateProvider auth, |
| 0 | 11 | | NavigationManager nav) |
| | 12 | | { |
| | 13 | | private HubConnection? _hubConnection; |
| | 14 | |
|
| | 15 | | public async Task StartAsync() |
| | 16 | | { |
| 0 | 17 | | if (_hubConnection is not null && _hubConnection.State != HubConnectionState.Disconnected) |
| 0 | 18 | | return; |
| | 19 | |
|
| 0 | 20 | | var apiUrl = configuration.GetSection("ApiUrl").Value!; |
| 0 | 21 | | _hubConnection = new HubConnectionBuilder() |
| 0 | 22 | | .WithUrl($"{apiUrl}/syki-hub", options => |
| 0 | 23 | | { |
| 0 | 24 | | options.HttpMessageHandlerFactory = innerHandler => |
| 0 | 25 | | new SykiDelegatingHandler(storage, auth, nav).WithInnerHandler(innerHandler); |
| 0 | 26 | | }) |
| 0 | 27 | | .WithAutomaticReconnect() |
| 0 | 28 | | .Build(); |
| | 29 | |
|
| 0 | 30 | | await _hubConnection.StartAsync(); |
| 0 | 31 | | } |
| | 32 | |
|
| | 33 | | public async Task StopAsync() |
| | 34 | | { |
| 0 | 35 | | if (_hubConnection is not null) |
| | 36 | | { |
| 0 | 37 | | await _hubConnection.StopAsync(); |
| 0 | 38 | | await _hubConnection.DisposeAsync(); |
| 0 | 39 | | _hubConnection = null; |
| | 40 | | } |
| 0 | 41 | | } |
| | 42 | | } |