// We accept multiple output binding types and rely on them to determine rest api actions // But in non .NET language, it's not able to convert JObject to different types // So need a converter to accurate convert JObject to either SignalRMessage or SignalRGroupAction public object ConvertToSignalROutput(object input) { if (input.GetType() != typeof(JObject)) { return(input); } var jobject = input as JObject; SignalRMessage message = null; if (messageConverter.TryConvert(jobject, out message)) { return(message); } SignalRGroupAction groupAction = null; if (groupActionConverter.TryConvert(jobject, out groupAction)) { return(groupAction); } throw new ArgumentException("Unable to convert JObject to valid output binding type, check parameters."); }
public Task RemoveUserFromAllGroups(SignalRGroupAction action) { var userId = action.UserId; if (string.IsNullOrEmpty(userId)) { throw new ArgumentException($"{nameof(userId)} cannot be null or empty"); } return(InvokeAsync(action.Endpoints, hubContext => hubContext.UserGroups.RemoveFromAllGroupsAsync(userId))); }
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."); } }
public Task RemoveConnectionFromGroup(SignalRGroupAction action) { var connectionId = action.ConnectionId; var groupName = action.GroupName; if (string.IsNullOrEmpty(connectionId)) { throw new ArgumentException($"{nameof(connectionId)} cannot be null or empty"); } if (string.IsNullOrEmpty(groupName)) { throw new ArgumentException($"{nameof(groupName)} cannot be null or empty"); } return(InvokeAsync(action.Endpoints, hubContext => hubContext.Groups.RemoveFromGroupAsync(connectionId, groupName))); }
public Task AddUserToGroup(SignalRGroupAction action) { var userId = action.UserId; var groupName = action.GroupName; if (string.IsNullOrEmpty(userId)) { throw new ArgumentException($"{nameof(userId)} cannot be null or empty"); } if (string.IsNullOrEmpty(groupName)) { throw new ArgumentException($"{nameof(groupName)} cannot be null or empty"); } return(InvokeAsync(action.Endpoints, hubContext => hubContext.UserGroups.AddToGroupAsync(userId, groupName))); }
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."); } }
public static SignalRGroupAction ToAzureFunctionsObject(SignalRGroupAction groupAction) { return(groupAction); }