Esempio n. 1
0
 private async Task BeginSignOut()
 {
     if (SignOutManager != null)
     {
         await SignOutManager.SetSignOutState();
         await NavigateToAsync("authentication/logout");
     }
 }
        public async Task <bool> LogoutAsync()
        {
            await _accessTokenService.ClearToken();

            ((ApiAuthenticationStateProvider)_authenticationStateProvider).MarkUserAsLoggedOut();
            _httpClient.DefaultRequestHeaders.Authorization = null;
            await _signOutManager.SetSignOutState();

            return(true);
        }
        public async Task InitConnectionAsync()
        {
            if (IsConnected)
            {
                return;
            }

            var accessTokenState = await _tokenProvider.RequestAccessToken();

            if (accessTokenState.TryGetToken(out var accessToken))
            {
                var apiBaseUrl        = _configurtion["api:baseUrl"];
                var accessTokenString = accessToken.Value;
                _hubConnection = new HubConnectionBuilder()
                                 .WithUrl($"{apiBaseUrl}tictactoe?access_token={accessTokenString}", options =>
                {
                    options.AccessTokenProvider = () => Task.FromResult(accessTokenString);
                })
                                 .WithAutomaticReconnect(new[] { TimeSpan.Zero, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(10) })
                                 .Build();


                _hubConnection.On("StartGame", (GameSession data) =>
                {
                    GameRunning?.Invoke(this, new GameRunningEventArgs {
                        Running = true
                    });
                    ActiveSession?.Invoke(this, new ActiveSessionEventArgs {
                        Session = data
                    });
                });

                _hubConnection.On("GameOver", (string data) =>
                {
                    GameRunning?.Invoke(this, new GameRunningEventArgs {
                        Running = false
                    });
                    ActiveSession?.Invoke(this, new ActiveSessionEventArgs {
                        Session = null
                    });
                    GameOver?.Invoke(this, new GameOverEventArgs {
                        WinnerId = data
                    });
                });

                _hubConnection.On <int>("Play", (data) =>
                {
                    Console.WriteLine($"Round Played. Data is {data}");
                    RoundPlayed?.Invoke(this, new GameEventArgs()
                    {
                        Value = data
                    });
                });

                _hubConnection.Closed += async(exception) => {
                    _toaster.Add(exception.Message, MatToastType.Danger);
                    await _sessionStateManager.SetSignOutState();

                    _navigationManager.NavigateTo("authentication/logout");
                };

                try
                {
                    Console.WriteLine("start signalr connection");
                    await _hubConnection.StartAsync();

                    _toaster.Add("Erfolgreich angemeldet.", MatToastType.Success, null, null, (options) => {
                        options.ShowProgressBar      = false;
                        options.ShowCloseButton      = false;
                        options.VisibleStateDuration = 2500;
                    });
                    await JoinNewSession();
                }
                catch (Exception e)
                {
                    _toaster.Add(
                        $"Beim Verbinden zum Server ist etwas schief gelaufen. Error: {e.Message}",
                        MatToastType.Danger);
                }
            }
        }
Esempio n. 4
0
        public async Task SignOut()
        {
            await _signOutManager.SetSignOutState();

            _navigation.NavigateTo("authentication/logout");
        }