簡體   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