I have single R version 2.2.1. I implement custom Id Provider
public class ChatUserIdProvider : IUserIdProvider
{
public string GetUserId(IRequest request)
{
if (request.User.Identity.IsAuthenticated)
{
Guid.Parse(request.User.Identity.GetUserId().ToString());
var userId = request.User.Identity.GetUserId().ToString();
return userId.ToString();
}
return "Un Known";
}
}
I made a simple chat app and every think OK, but when I try to send a message to multi users the client event not firing here is hub function
public void SendToMany(string msg, List<string> userIds)
{
try
{
//db things here
Clients.Users(userIds).sendMessage(msg);
}
catch (Exception ex)
{
Logs.Log(ex);
}
}
Startup
GlobalHost.DependencyResolver.Register(typeof(IUserIdProvider), () => new ChatUserIdProvider ());
app.MapSignalR();
Js
$(function () {
var chat = $.connection.chatHub;
chat.client.sendMessage= function (msg) {
$('.msgs').append('<div>'+ group.Name + '</div>');
$('#' + group.Id).click();
}
$.connection.hub.start();
})
function BrodCast() {
try {
var chatids = new Array();
$('.ckusergr').each(function () {
if ($(this).is(':checked')) {
chatids.push($(this).attr('iid'));
}
})
chat.server.sendToMany($('.txtmsg').val(), chatids);
} catch (e) {
console.log(e)
}
}
the problem with this line
public void SendToMany(string msg, List<string> userIds)
{
try
{
//db things here
Clients.Users(userIds).sendMessage(msg); // Her is the Problem
}
catch (Exception ex)
{
Logs.Log(ex);
}
}
if I change to become like this every thing work great.
public void SendToMany(string msg, List<string> userIds)
{
try
{
//db things here
foreach (string item in userIds)
{
Clients.User(item).sendMessage(msg);
}
}
catch (Exception ex)
{
Logs.Log(ex);
}
}
I had similar problem today and I find out when I send as parameter
List<string>
which contains all usernames intoClients.Users(list of usernames)
somehow it will work also.I found this by accident, maybe someone with better experiences may clarify why this is working since this should only accept
IList<string> userIds
hub
before declaring 'send' function.Broadcast function
inside the main function which is declaring variable chat.something like this should work :