예제 #1
0
        public override async Task OnActivateAsync()
        {
            await base.OnActivateAsync();

            _hubContext = GrainFactory.GetHub <IHeroHub>();

            if (State.Hero == null)
            {
                // todo: use key data
                var keySplit = PrimaryKey.Split('/');
                _keyData.Tenant  = keySplit[1];
                _keyData.HeroKey = keySplit[2];
                var hero = await _heroDataClient.GetByKey(_keyData.HeroKey);
                await Set(hero);
            }

            var hubGroup    = _hubContext.Group($"hero:{_keyData.HeroKey}");
            var hubAllGroup = _hubContext.Group($"hero:all");

            var streamProvider = GetStreamProvider(Constants.STREAM_PROVIDER);
            var stream         = streamProvider.GetStream <Hero>(StreamConstants.HeroStream, $"hero:{_keyData.HeroKey}");

            RegisterTimer(async x =>
            {
                State.Hero.Health = RandomUtils.GenerateNumber(1, 100);

                await Task.WhenAll(
                    Set(State.Hero),
                    stream.OnNextAsync(State.Hero),
                    hubGroup.SendSignalRMessage("HeroChanged", State.Hero),
                    hubAllGroup.SendSignalRMessage("HeroChanged", State.Hero)
                    );
            }, State, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(3));
        }
예제 #2
0
        public async Task SendMessageAsync(string name, string message)
        {
            var groupId = this.GetPrimaryKeyString();

            _logger.LogInformation($"{nameof(SendMessageAsync)} called. Name:{name}, Message:{message}, Key:{groupId}");
            _logger.LogInformation($"Sending message to group: {groupId}. MethodName:{BroadcastMessage} Name:{name}, Message:{message}");

            await _hubContext.Group(groupId).Send(BroadcastMessage, name, message);
        }
예제 #3
0
        public override async Task OnActivateAsync()
        {
            await base.OnActivateAsync();

            _hubContext = GrainFactory.GetHub <IHeroHub>();
            _keyData    = this.ParseKey <HeroKeyData>(HeroKeyData.Template);

            if (State.Entity == null)
            {
                var entity = await _heroDataClient.GetByKey(_keyData.Id);

                if (entity == null)
                {
                    return;
                }

                await Set(entity);
            }

            var hubGroup    = _hubContext.Group($"{_keyData.Tenant}/hero/{_keyData.Id}");
            var hubAllGroup = _hubContext.Group($"{_keyData.Tenant}/hero");             // all

            var streamProvider = GetStreamProvider(Constants.STREAM_PROVIDER);
            var stream         = streamProvider.GetStream <Hero>(StreamConstants.HeroStream, $"hero:{_keyData.Id}");

            RegisterTimer(async x =>
            {
                State.Entity.Health = RandomUtils.GenerateNumber(1, 100);

                await Task.WhenAll(
                    Set(State.Entity),
                    stream.OnNextAsync(State.Entity),
                    hubGroup.Send("HeroChanged", State.Entity),
                    hubAllGroup.Send("HeroChanged", State.Entity)
                    );
            }, State, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(3));
        }