Example #1
0
        public override async Task SendConnectionAsync(string connectionId, string methodName, object[] args, CancellationToken cancellationToken = default)
        {
            if (connectionId == null)
            {
                throw new ArgumentNullException(nameof(connectionId));
            }

            // If the connection is local we can skip sending the message through the bus since we require sticky connections.
            // This also saves serializing and deserializing the message!
            var connection = Connections[connectionId];

            if (connection != null)
            {
                // Connection is local, so we can skip publish
                await connection.WriteAsync(new InvocationMessage(methodName, args), cancellationToken).AsTask();

                return;
            }

            using IHubLifetimeScope <THub> scope = _scopeProvider.CreateScope <THub>();
            LogContext.Info?.Log("Publishing Connection<THub> message to MassTransit.");
            await scope.PublishEndpoint.Publish <Connection <THub> >(new
            {
                ConnectionId = connectionId,
                Messages     = Protocols.ToProtocolDictionary(methodName, args)
            },
                                                                     cancellationToken);
        }
Example #2
0
 public override async Task SendAllAsync(string methodName, object[] args, CancellationToken cancellationToken = default)
 {
     using IHubLifetimeScope <THub> scope = _scopeProvider.CreateScope <THub>();
     LogContext.Info?.Log("Publishing All<THub> message to MassTransit.");
     await scope.PublishEndpoint.Publish <All <THub> >(
         new { Messages = Protocols.ToProtocolDictionary(methodName, args) }, cancellationToken);
 }
Example #3
0
 public override async Task SendAllExceptAsync(string methodName, object[] args, IReadOnlyList <string> excludedConnectionIds,
                                               CancellationToken cancellationToken = default)
 {
     using IHubLifetimeScope <THub> scope = _scopeProvider.CreateScope <THub>();
     LogContext.Info?.Log("Publishing All<THub> message to MassTransit, with exceptions.");
     await scope.PublishEndpoint.Publish <All <THub> >(new
     {
         Messages = Protocols.ToProtocolDictionary(methodName, args),
         ExcludedConnectionIds = excludedConnectionIds.ToArray()
     }, cancellationToken);
 }
Example #4
0
        public override async Task SendGroupAsync(string groupName, string methodName, object[] args, CancellationToken cancellationToken = default)
        {
            if (groupName == null)
            {
                throw new ArgumentNullException(nameof(groupName));
            }

            using IHubLifetimeScope <THub> scope = _scopeProvider.CreateScope <THub>();
            LogContext.Info?.Log("Publishing Group<THub> message to MassTransit.");
            await scope.PublishEndpoint.Publish <Group <THub> >(new
            {
                GroupName = groupName,
                Messages  = Protocols.ToProtocolDictionary(methodName, args)
            },
                                                                cancellationToken);
        }
Example #5
0
        public override async Task RemoveFromGroupAsync(string connectionId, string groupName, CancellationToken cancellationToken = default)
        {
            if (connectionId == null)
            {
                throw new ArgumentNullException(nameof(connectionId));
            }

            if (groupName == null)
            {
                throw new ArgumentNullException(nameof(groupName));
            }

            var connection = Connections[connectionId];

            if (connection != null)
            {
                // short circuit if connection is on this server
                RemoveGroupAsyncCore(connection, groupName);

                return;
            }

            // Publish to mass transit group management instead, but it waits for an ack...
            using IHubLifetimeScope <THub> scope = _scopeProvider.CreateScope <THub>();
            try
            {
                LogContext.Info?.Log("Publishing remove GroupManagement<THub> message to MassTransit.");
                RequestHandle <GroupManagement <THub> > request = scope.RequestClient.Create(new
                {
                    ConnectionId = connectionId,
                    GroupName    = groupName,
                    ServerName,
                    Action = GroupAction.Remove
                },
                                                                                             cancellationToken);

                Response <Ack <THub> > ack = await request.GetResponse <Ack <THub> >();

                LogContext.Info?.Log($"Request Received for remove GroupManagement<THub> from {ack.Message.ServerName}.");
            }
            catch (RequestTimeoutException e)
            {
                // That's okay, just log and swallow
                LogContext.Warning?.Log(e, "GroupManagement<THub> remove ack timed out.", e);
            }
        }
Example #6
0
        public override async Task SendUsersAsync(IReadOnlyList <string> userIds, string methodName, object[] args,
                                                  CancellationToken cancellationToken = default)
        {
            if (userIds == null)
            {
                throw new ArgumentNullException(nameof(userIds));
            }

            if (userIds.Any())
            {
                using IHubLifetimeScope <THub> scope = _scopeProvider.CreateScope <THub>();
                IReadOnlyDictionary <string, byte[]> protocolDictionary = Protocols.ToProtocolDictionary(methodName, args);
                IEnumerable <Task> publishTasks = userIds.Select(userId => scope.PublishEndpoint.Publish <User <THub> >(new
                {
                    UserId   = userId,
                    Messages = protocolDictionary
                }, cancellationToken));

                LogContext.Info?.Log("Publishing multiple User<THub> messages to MassTransit.");
                await Task.WhenAll(publishTasks);
            }
        }