简体   繁体   English

向SignalR中的特定用户发送值

[英]send value to specific User in SignalR

I have 4 textboxes that are filling in same time after user push send button Using SignalR. 用户按下“使用SignalR”发送按钮后,我同时填写了4个文本框。 In one part I am sending textbox values to all users and this is working. 在一部分中,我正在将文本框值发送给所有用户,并且此操作有效。 But when I try to send the values to specific user, after pushing the button, all textboxes get empty. 但是,当我尝试将值发送给特定用户时,按下按钮后,所有文本框都为空。

This is the code that is working, sending values to all users: 这是起作用的代码,它向所有用户发送值:

This is 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);

    }

and this is js: 这是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);
    };

but when I try to send values to specific user it is not working: as I am debussing the C# code is working I thing the problem is in JS: 但是,当我尝试将值发送给特定用户时,它不起作用:由于我正在删除C#代码,因此问题出在JS:

this is C# method that send values of text boxes to specific user: 这是将文本框的值发送给特定用户的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
                });
            }
        }
    }

it will call the Private part in JS file that is : 它将调用JS文件中的Private部分,即:

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

The function signatures do not match. 功能签名不匹配。 There are different number of paramerters, they have to match exactly or the javascript function will never be called, it looks like you are trying use the parameters for the send function, when you are calling the broadcastMessage function. 参数的数量不同,它们必须完全匹配,否则javascript函数将永远不会被调用,当您调用broadcastMessage函数时,您似乎正在尝试使用send函数的参数。

In the JavaScript for the user you are looking for: 在要查找的用户的JavaScript中:

function (loanType, loanAmount, interestRates, payment)

But you are sending: 但是您正在发送:

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

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

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