Exemplo n.º 1
0
        private async Task <object> PullMessagesAsync(string projectId, string subscriptionId, bool acknowledge)
        {
            var subscriptionName = new SubscriptionName(projectId, subscriptionId);
            var subscription     = await SubscriberClient.CreateAsync(subscriptionName);

            await subscription.StartAsync(
                async (PubsubMessage message, CancellationToken cancel) =>
            {
                string json = Encoding.UTF8.GetString(message.Data.ToArray());

                await Console.Out.WriteLineAsync($"Message {message.MessageId}: {json}");

                // Insert or update author
                IncreasingCP increasingCP = JsonConvert.DeserializeObject <IncreasingCP>(json);
                _userRepository.IncreaseContributionPoint(increasingCP.UserId, increasingCP.Point);

                return(acknowledge ? SubscriberClient.Reply.Ack : SubscriberClient.Reply.Nack);
            });

            //// Run for 3 seconds.
            //await Task.Delay(3000);
            //await subscription.StopAsync(CancellationToken.None);

            // [END pubsub_subscriber_async_pull]
            return(0);
        }
Exemplo n.º 2
0
        public async void PublishCP(IncreasingCP increasingCP)
        {
            var topicName = new TopicName(_pubsubSettings.Value.ProjectId, _pubsubSettings.Value.TopicCP);

            PublisherClient publisher = await PublisherClient.CreateAsync(topicName);

            var json    = JsonConvert.SerializeObject(increasingCP);
            var message = new PubsubMessage()
            {
                Data = ByteString.CopyFromUtf8(json)
            };

            // Publish it
            var response = publisher.PublishAsync(message);
        }