示例#1
0
        async Task OnMessage(string[] parts)
        {
            string id      = parts[1];
            Type   type    = Type.GetType(parts[2]);
            var    message = JsonConvert.DeserializeObject(parts[3], type) as IPubSubMessage;

            message.RemoteConnectionId = this.Id;

            try
            {
                await _hub.Publish(message);
                await SendOkResponse(id);
            }
            catch (Exception ex)
            {
                _logger.Debug(ex);
                await SendErrorResponse(id, ex);
            }
        }
示例#2
0
        internal async Task Put(IReadOnlyCollection <Subscriber> subscribers, IPubSubMessage message)
        {
            bool             hasAtLeastOneSubscriber = false;
            List <Exception> exceptions = null;

            // deliver to local subscribers
            foreach (var item in subscribers)
            {
                try
                {
                    if (item.Subscription.ShouldDeliver(message))
                    {
                        hasAtLeastOneSubscriber = true;
                        await item.Subscription.Deliver(message);
                    }
                }
                catch (Exception ex)
                {
                    if (exceptions == null)
                    {
                        exceptions = new List <Exception>();
                    }
                    exceptions.Add(ex);
                }
            }

            // deliver to remote subscribers
            bool remoteDeliver = false;

            try
            {
                if (await _onRemoteDeliver(message))
                {
                    remoteDeliver = hasAtLeastOneSubscriber = true;
                }
            }
            catch (Exception ex)
            {
                if (exceptions == null)
                {
                    exceptions = new List <Exception>();
                }
                exceptions.Add(ex);
            }

            //WriteDebugLine($"Published <<<{message.GetType().Name}>>> Subs: {subscribers.Count}, Remote: {remoteDeliver}, Ex: {exceptions?.Count ?? 0}");

            if (exceptions != null)
            {
                throw new AggregateException(exceptions).Fix();
            }

            if (message.DeliverAtLeastOnce && !hasAtLeastOneSubscriber)
            {
                if (message.WaitForSubscriberTimeout > 0)
                {
                    _logger.Debug($"Delayed <<<{message.GetType().Name}>>> for {message.WaitForSubscriberTimeout} ms");
                    await _onDelayedMessage(message);
                }
                else
                {
                    throw new NoSubscribersException("Failed to deliver the message - no one is listening");
                }
            }
        }