Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法通过信号器中的ConnectionId向特定用户发送消息_C#_Asp.net Mvc_Signalr_Signalr Hub - Fatal编程技术网

C# 无法通过信号器中的ConnectionId向特定用户发送消息

C# 无法通过信号器中的ConnectionId向特定用户发送消息,c#,asp.net-mvc,signalr,signalr-hub,C#,Asp.net Mvc,Signalr,Signalr Hub,当我尝试像这样发送所有用户时,我无法通过connectionId将消息发送给特定用户:context.Clients.all.updateMessageMessages-此代码正在工作。 hare是集线器代码: public void Send(string userToId, string userForId, string message) { //Get Recipent (userIdfor) connectionId va

当我尝试像这样发送所有用户时,我无法通过connectionId将消息发送给特定用户:context.Clients.all.updateMessageMessages-此代码正在工作。 hare是集线器代码:

public void Send(string userToId, string userForId, string message)
        {
            //Get Recipent (userIdfor) connectionId
            var signalrhelper = new HomeController();
            string userForconnectionId = signalrhelper.GetConnecionIdByUserId(userForId);

            IHubContext context = GlobalHost.ConnectionManager.GetHubContext<ChatHubs>();

            string messageSenderConnId= signalrhelper.GetConnecionIdByUserId(userToId);

            //Call Receiver
            context.Clients.Client(userForconnectionId).updateMessages(message);
            //Call Sender
            context.Clients.Client(messageSenderConnId).updateMessages(message);
        }

有人知道发生了什么吗?

按用户,我想你指的是经过身份验证的用户?如果是这种情况,您必须首先将连接映射到用户。例如,一个用户可以有2个或更多信号器连接。因此,第一步是将用户映射到连接,然后您可以向用户发送一条消息,他/她的所有连接代理都将收到该消息


有几种方法可以将连接映射到用户,指南如下:

尽管这篇文章已经很老了:我昨天也遇到了类似的问题,花了我几个小时!我有连接ID,但没有客户收到通知。上下文。客户。所有。方法。。。很好,但是上下文。客户。客户端ID。方法。。。不起作用。我最终意识到,我将连接ID作为uniqueIdentifier存储在数据库中,MS SQL将uniqueIdentifier值转换为大写,因此ConnectionID不再有效。在发布到我的连接客户端之前,我将CONNECTINID转换为小写,然后它就工作了……也许你也遇到了类似的问题。但是你的帖子因为空白而连接ID无效,这有助于我找到问题。

谢谢,我已经解决了这个问题。我的问题是db中ConnectionId的结尾。我修剪了一下,一切都很好。
$(function() {
        // Declare a proxy to reference the hub.
        var notifications = $.connection.chatHubs;

        // Create a function that the hub can call to broadcast messages.
        notifications.client.updateMessages = function(data) {

            if (window.location.href.indexOf("Messages/DetailMessage?userId") > -1) {
                $('#timeline-messages').append('{0}'.Stringformat(data));
            } else {
                ReplaceUpdateTargetIdToReturnData("Messages/GetMessages", "#header_inbox_bar", "#systemMessage");
            }

        };
        $.connection.hub.start().done(function() {

            var myClientId = $.connection.hub.id;
            GetConnectionIdToSignalR("Home", "SaveConnectionIdbyUserName", "userId", @Session["UserId"], "hubConnectionId", myClientId);


            $('#sendMessageButton').click(function() {
                if ($('#sendMessageFiled').val().length > 1) {
                    // Call the Send method on the hub.
                    notifications.server.send(@Session["UserId"], myClientId, $('#sendMessageButton').attr("title"), $('#sendMessageFiled').val());
                    // Clear text box and reset focus for next comment.
                    $('#sendMessageFiled').val('').focus();
                } else {
                    $('#sendMessageFiled').focus();
                }
            });
        }).fail(function (e) {
            alert(e);
        });
    });