Exemplo n.º 1
0
        public async Task AddAsync(T item, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (item == null)
            {
                throw new ArgumentNullException("Binding Object");
            }

            var convertItem = converter.ConvertToSignalROutput(item);

            if (convertItem.GetType() == typeof(SignalRMessage))
            {
                SignalRMessage message = convertItem as SignalRMessage;
                var            data    = new SignalRData
                {
                    Target    = message.Target,
                    Arguments = message.Arguments
                };

                if (!string.IsNullOrEmpty(message.ConnectionId))
                {
                    await client.SendToConnection(hubName, message.ConnectionId, data).ConfigureAwait(false);
                }
                else if (!string.IsNullOrEmpty(message.UserId))
                {
                    await client.SendToUser(hubName, message.UserId, data).ConfigureAwait(false);
                }
                else if (!string.IsNullOrEmpty(message.GroupName))
                {
                    await client.SendToGroup(hubName, message.GroupName, data).ConfigureAwait(false);
                }
                else
                {
                    await client.SendToAll(hubName, data).ConfigureAwait(false);
                }
            }
            else if (convertItem.GetType() == typeof(SignalRGroupAction))
            {
                SignalRGroupAction groupAction = convertItem as SignalRGroupAction;

                if (!string.IsNullOrEmpty(groupAction.ConnectionId))
                {
                    if (groupAction.Action == GroupAction.Add)
                    {
                        await client.AddConnectionToGroup(hubName, groupAction.ConnectionId, groupAction.GroupName).ConfigureAwait(false);
                    }
                    else
                    {
                        await client.RemoveConnectionFromGroup(hubName, groupAction.ConnectionId, groupAction.GroupName).ConfigureAwait(false);
                    }
                }
                else if (!string.IsNullOrEmpty(groupAction.UserId))
                {
                    if (groupAction.Action == GroupAction.Add)
                    {
                        await client.AddUserToGroup(hubName, groupAction.UserId, groupAction.GroupName).ConfigureAwait(false);
                    }
                    else
                    {
                        await client.RemoveUserFromGroup(hubName, groupAction.UserId, groupAction.GroupName).ConfigureAwait(false);
                    }
                }
                else
                {
                    throw new ArgumentException($"ConnectionId and UserId cannot be null or empty together");
                }
            }
            else
            {
                throw new ArgumentException("Unsupport Binding Type.");
            }
        }
Exemplo n.º 2
0
        public async Task AddAsync(T item, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            var convertItem = _converter.ConvertToSignalROutput(item);

            if (convertItem.GetType() == typeof(SignalRMessage))
            {
                var message = convertItem as SignalRMessage;
                var data    = new SignalRData
                {
                    Target    = message.Target,
                    Arguments = message.Arguments,
                    Endpoints = message.Endpoints
                };

                if (!string.IsNullOrEmpty(message.ConnectionId))
                {
                    await _client.SendToConnection(message.ConnectionId, data).ConfigureAwait(false);
                }
                else if (!string.IsNullOrEmpty(message.UserId))
                {
                    await _client.SendToUser(message.UserId, data).ConfigureAwait(false);
                }
                else if (!string.IsNullOrEmpty(message.GroupName))
                {
                    await _client.SendToGroup(message.GroupName, data).ConfigureAwait(false);
                }
                else
                {
                    await _client.SendToAll(data).ConfigureAwait(false);
                }
            }
            else if (convertItem.GetType() == typeof(SignalRGroupAction))
            {
                var groupAction = convertItem as SignalRGroupAction;

                if (!string.IsNullOrEmpty(groupAction.ConnectionId))
                {
                    switch (groupAction.Action)
                    {
                    case GroupAction.Add:
                        await _client.AddConnectionToGroup(groupAction).ConfigureAwait(false);

                        break;

                    case GroupAction.Remove:
                        await _client.RemoveConnectionFromGroup(groupAction).ConfigureAwait(false);

                        break;
                    }
                }
                else if (!string.IsNullOrEmpty(groupAction.UserId))
                {
                    switch (groupAction.Action)
                    {
                    case GroupAction.Add:
                        await _client.AddUserToGroup(groupAction).ConfigureAwait(false);

                        break;

                    case GroupAction.Remove:
                        await _client.RemoveUserFromGroup(groupAction).ConfigureAwait(false);

                        break;

                    case GroupAction.RemoveAll:
                        await _client.RemoveUserFromAllGroups(groupAction).ConfigureAwait(false);

                        break;
                    }
                }
                else
                {
                    throw new ArgumentException($"ConnectionId and UserId cannot be null or empty together");
                }
            }
            else
            {
                throw new ArgumentException("Unsupport Binding Type.");
            }
        }