// 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 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 SendMessage(string hubName, SignalRMessage message) { var connectionInfo = GetServerConnectionInfo(hubName); return(PostJsonAsync(connectionInfo.Url, message, connectionInfo.AccessToken)); }
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 SignalRMessage ToAzureFunctionsObject(SignalRMessage signalRMessage) { return(signalRMessage); }