signalr Clients.Users(userIds).send(msg) not firin

2019-09-08 12:21发布

问题:

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);
        }
    }

回答1:

  • First, you need to start your hub before declaring 'send' function.
  • Second, you should put your Broadcast function inside the main function which is declaring variable chat.

something like this should work :

$(function () {
   var chat = $.connection.chatHub;
    chat.client.sendMessage= function (msg) {
        $('.msgs').append('<div>'+ group.Name + '</div>');
        $('#' + group.Id).click();
    }
   $.connection.hub.start().done(function () {
        var chatids = new Array();
        $('.ckusergr').each(function () {
            if ($(this).is(':checked')) {
                chatids.push($(this).attr('iid'));
            }
        })
        chat.server.sendToMany($('.txtmsg').val(), chatids);
    });
})


回答2:

I had similar problem today and I find out when I send as parameter List<string> which contains all usernames into Clients.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



标签: c# signalr