0
我有4個文本框在用戶按下發送按鈕後同時填充使用SignalR。在一部分,我發送文本框值給所有用戶,這是行得通的。但是,當我嘗試將值發送給特定用戶時,按下按鈕後,所有文本框都會變空。將值發送給SignalR中的特定用戶
這是工作的代碼,發送值給所有用戶:
這是集線器:
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);
};
但是當我嘗試發送值給特定的用戶它不工作:因爲我debussing 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文件,該文件是:
if (viewModel.isInPrivateChat()) {
$.connection.hub.start();
chatHub.server.send(msgValue, viewModel.privateChatUser(), $('#txtLoanType option:selected').val(), $('#txtLoanAmount').val(), $('#txtInterestRates').val(), $('#txtPayment').val());