将值发送给SignalR中的特定用户

时间:2017-05-03 23:18:22

标签: javascript c# signalr

我有4个文本框在用户按下发送按钮后同时填写使用SignalR。在一个部分中,我将向所有用户发送文本框值,这是有效的。但是当我尝试将值发送给特定用户时,按下按钮后,所有文本框都变空。

这是正在运行的代码,将值发送给所有用户:

这是Hub:

 public void Send(string loanType, string loanAmount, string interestRates, string payment)
    {
        User sender = GetUser(Context.User.Identity.Name);

        var username = sender.Name;
        IEnumerable<string> connectionIds = sender.ConnectionIds;

        //All connected clients.
        Clients.All.broadcastMessage(loanType, loanAmount, interestRates, payment);

    }

这是js:

  $('#sendmessage').click(function (e) {
            sendPayment();
            e.preventDefault();
        });


        function sendPayment() {
        var msgValue = $msgTxt.val();
        var loanType = $('#txtLoanType').val();
        var loanAmount = $('#txtLoanAmount').val();
        var interestRates = $('#txtInterestRates').val();
        var payment = $('#txtPayment').val();

        if (loanType !== null && loanType.length > 0 && loanAmount !== null && loanAmount.length > 0 && interestRates !== null && interestRates.length > 0
            && payment !== null && payment.length > 0) {

            if (viewModel.isInPrivateChat()) {
                $.connection.hub.start();
                chatHub.server.send(msgValue, viewModel.privateChatUser(), $('#txtLoanType option:selected').val(), $('#txtLoanAmount').val(), $('#txtInterestRates').val(), $('#txtPayment').val());


            }
            else {

                 // Call the Send method on the hub.
                chatHub.server.send($('#txtLoanType option:selected').val(), $('#txtLoanAmount').val(), $('#txtInterestRates').val(), $('#txtPayment').val());


            }
        }



     chatHub.client.broadcastMessage = function (loanType, loanAmount, interestRates, payment) {
            $('#txtLoanType').val(loanType);
            $('#txtLoanAmount').val(loanAmount);
            $('#txtInterestRates').val(interestRates);
            $('#txtPayment').val(payment);
    };

但是当我尝试向特定用户发送值时,它无法正常工作:因为我正在讨论C#代码正在工作我的问题在于JS:

这是将文本框的值发送给特定用户的C#方法:

 public void Send(string message, string to, string loanType, string loanAmount, string interestRates, string payment)
    {
        User receiver;
        if (Users.TryGetValue(to, out receiver))
        {

            User sender = GetUser(Context.User.Identity.Name);

            IEnumerable<string> allReceivers;
            lock (receiver.ConnectionIds)
            {
                lock (sender.ConnectionIds)
                {

                    allReceivers = receiver.ConnectionIds.Concat(sender.ConnectionIds);
                }
            }

            foreach (var cid in allReceivers)
            {
                Clients.Client(cid).broadcastMessage(new { message = message, isPrivate = true, loanType = loanType,
                    loanAmount = loanAmount,
                    interestRates = interestRates,
                    payment = payment
                });
            }
        }
    }

它将调用JS文件中的Private部分:

               if (viewModel.isInPrivateChat()) {
                $.connection.hub.start();
                chatHub.server.send(msgValue, viewModel.privateChatUser(), $('#txtLoanType option:selected').val(), $('#txtLoanAmount').val(), $('#txtInterestRates').val(), $('#txtPayment').val());

1 个答案:

答案 0 :(得分:0)

功能签名不匹配。有不同数量的参数,它们必须完全匹配或者javascript函数永远不会被调用,看起来你正在尝试使用send函数的参数,当你调用broadcastMessage函数时。

在您正在寻找的用户的JavaScript中:

function (loanType, loanAmount, interestRates, payment)

但是你发送了:

broadcastMessage(new { message = message, isPrivate = true, loanType = loanType,
                loanAmount = loanAmount,
                interestRates = interestRates,
                payment = payment
            })