示例#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.UserId) && !string.IsNullOrEmpty(message.GroupName))
                {
                    throw new ArgumentException("GroupName and UserId can not be specified at the same time.");
                }

                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 (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("Unsupport Binding Type.");
            }
        }
示例#2
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(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))
            {
                SignalRGroupAction groupAction = convertItem as SignalRGroupAction;

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

                        break;

                    case GroupAction.Remove:
                        await client.RemoveConnectionFromGroup(groupAction.ConnectionId, groupAction.GroupName).ConfigureAwait(false);

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

                        break;

                    case GroupAction.Remove:
                        await client.RemoveUserFromGroup(groupAction.UserId, groupAction.GroupName).ConfigureAwait(false);

                        break;

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

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