Exemple #1
0
        private async Task StartWatcherAsync()
        {
            // observing a hardcoded player id for sample purposes
            // the load generator will update player ids within a range that includes this player
            var        playerId = new Guid("{2349992C-860A-4EDA-9590-000000000006}");
            var        player   = client.GetGrain <IPlayerGrain>(playerId);
            IGameGrain game     = null;

            // poll for this player to join a game
            while (game == null)
            {
                logger.LogInformation("Getting current game for player {@PlayerId}...",
                                      playerId);

                try
                {
                    game = await player.GetCurrentGameAsync();
                }
                catch (Exception error)
                {
                    logger.LogError(error,
                                    "Error while requesting current game for player {@PlayerId}",
                                    playerId);
                }

                if (game == null)
                {
                    try
                    {
                        await Task.Delay(1000, startCancellation.Token);
                    }
                    catch (OperationCanceledException)
                    {
                        return;
                    }
                }
            }

            logger.LogInformation("Observing updates for game {@GameKey}",
                                  game.GetPrimaryKey());

            // subscribe for updates
            var watcher = client.ServiceProvider.GetService <IGameObserver>();
            await game.ObserveGameUpdatesAsync(
                await client.CreateObjectReference <IGameObserver>(watcher));

            logger.LogInformation("Subscribed successfully to game {@GameKey}",
                                  game.GetPrimaryKey());
        }
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            // the load generator will update player ids within a range that includes this player
            var playerId = new Guid("{2349992C-860A-4EDA-9590-000000000006}");
            var player   = _client.GetGrain <IPlayerGrain>(playerId);

            // poll for this player to join a game
            while (_game == null)
            {
                _logger.LogInformation("Getting current game for player {@PlayerId}...",
                                       playerId);

                try
                {
                    _game = await player.GetCurrentGameAsync();
                }
                catch (Exception error)
                {
                    _logger.LogError(error,
                                     "Error while requesting current game for player {@PlayerId}",
                                     playerId);
                }

                if (_game == null)
                {
                    try
                    {
                        await Task.Delay(1000, cancellationToken);
                    }
                    catch (OperationCanceledException)
                    {
                        return;
                    }
                }
            }

            _logger.LogInformation("Observing updates for game {@GameKey}",
                                   _game.GetPrimaryKey());

            // subscribe for updates
            var reference = await _client.CreateObjectReference <IGameObserver>(_observer);

            await _game.ObserveGameUpdatesAsync(reference);

            _logger.LogInformation("Subscribed successfully to game {@GameKey}",
                                   _game.GetPrimaryKey());
        }