I use signalR hub to send messages and files in my web API service chat. I write a method to handle how messages send to clients as broadcast, user, or group :
public class MessageHub : Hub<IMessageHub>,IMessage
{
private readonly static ConnectionMapping<string> _connections = new ConnectionMapping<string>();
private readonly static GroupMapping<string> _groups = new GroupMapping<string>();
public async Task MessageDispatcher(ChatMessage message)
{
message.User = GetUserName();
// Broadcast message
if (string.IsNullOrEmpty(message.SendToGroup) && string.IsNullOrEmpty(message.SendDirectToUser))
await Clients.AllExcept(Context.ConnectionId).SendMessage(message);//send all except current user
//Send direct to user
if (!string.IsNullOrEmpty(message.SendDirectToUser))
{
var connectionIds = _connections.GetConnections(message.SendDirectToUser);
foreach (string userConnectionId in connectionIds)
{
await Clients.Client(userConnectionId).SendMessage(message);
}
}
//Send to group
if (!string.IsNullOrEmpty(message.SendToGroup))
{
foreach (var user in _groups.GetUsers(message.SendToGroup))
{
var connectionIds = _connections.GetConnections(user);
foreach (string userConnectionId in connectionIds)
{
await Groups.AddToGroupAsync(userConnectionId, message.SendToGroup);
}
}
await Clients.GroupExcept(message.SendToGroup, Context.ConnectionId).SendMessage(message);
}
}
}
Now I add a controller to my web API app to transfer files between clients because SignalR doesn't support file transfer as I found.
I want to call my MessageDispacher method to notify another user as my client wants but I don't have access to MessageDispacher.
any solution?