简体   繁体   中英

send value to specific User in SignalR

I have 4 textboxes that are filling in same time after user push send button Using SignalR. 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:

  $('#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:

this is C# method that send values of text boxes to specific user:

 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 :

               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.

In the JavaScript for the user you are looking for:

function (loanType, loanAmount, interestRates, payment)

But you are sending:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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