繁体   English   中英

无法通过SignalR中的ConnectionId向特定用户发送消息

[英]Can not send message to specific user By ConnectionId in SignalR

当我尝试发送所有这样的用户时,我无法通过connectionId向特定用户发送消息:context.Clients.All.updateMessages(message) - 此代码正常工作。 野兔是枢纽代码:

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

野兔是我的观点:

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

任何人都可以知道发生了什么吗?

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

有几种方法可以将连接映射到用户,指南在这里: http//www.google.co.uk/url?q = http://www.asp.net/signalr/overview/guide-to-the -API /映射用户到连接&SA = U&EI = TJJ-VJuPMsPBOZGGgYgH&VED = 0CAsQFjAA&USG = AFQjCNFXoGJOm3mzenAJbz46TUq-Lx2bvA

虽然这篇文章已经过时了:昨天我遇到了类似的问题,花了我几个小时! 我有connectionIds但没有客户收到通知。 Context.Clients.All.aMethod(...)运行正常,但Context.Clients.Client(theid).aMethod(...)不起作用。 我终于意识到我将connection-id作为uniqueIdentifier存储在数据库中,MS SQL将uniqueIdentifier值转换为大写,因此连接不再有效。 我将connectinIds转换为小写,然后发布到我的连接客户端然后它工作了......也许你遇到了类似的问题。 但是由于空白而你的连接无效的帖子帮我发现了问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM