Exemplo n.º 1
0
        public Task Open(string json)
        {
            _logger.LogTrace("Open called on TelemetryHub");
            var subscription = JsonConvert.DeserializeObject <SubscriptionInfo>(json);

            if (_users.ContainsKey(Context.ConnectionId))
            {
                var actualUser = _users[Context.ConnectionId];

                int maxConcurrentCalls = 5;

                _logger.LogInformation("Subscription request received: {0}", JsonConvert.SerializeObject(subscription));

                _messageProcessor.CreateServiceBusConnectionString(
                    subscription.CreationResponse.ConnectionUrl,
                    subscription.SubscriptionTopic,
                    subscription.CreationResponse.SasToken,
                    maxConcurrentCalls
                    );

                var subscriptionId = Guid.NewGuid().ToString();

                _messageProcessor.RegisterOnMessageHandlerAndReceiveMessages(_context, subscription.CreationResponse.SubscriptionId, Context.ConnectionId);

                _connectionHandler.RegisterMessageProcessor(subscriptionId, _messageProcessor);

                UserHubSubscription userHubSubscription = new UserHubSubscription
                {
                    SubscriptionId         = subscription.CreationResponse.SubscriptionId,
                    SubscriptionType       = subscription.SubscriptionTopic,
                    MessageProcessorId     = subscriptionId,
                    DataAccessUrl          = subscription.DataAccessApiUrl,
                    OcpApimSubscriptionKey = subscription.OcpApimSubscriptionKey,
                    Token = subscription.Token
                };

                actualUser.UserHubSubscription.Add(userHubSubscription);

                _users.AddOrUpdate(Context.ConnectionId, actualUser, (key, oldValue) =>
                {
                    return(actualUser);
                });
            }
            var args = new object[] { subscription.CreationResponse.SubscriptionId };

            return(Clients.Caller.SendCoreAsync("opened", args));
        }