[英]Can not send message to specific user By ConnectionId in SignalR
i can not send message to specific user by connectionId when I try to send all users like this: context.Clients.All.updateMessages(message) - this code is working. 当我尝试发送所有这样的用户时,我无法通过connectionId向特定用户发送消息:context.Clients.All.updateMessages(message) - 此代码正常工作。 hare is Hub code: 野兔是枢纽代码:
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);
}
Hare is My View: 野兔是我的观点:
$(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);
});
});
Can anybody Know what's happen ? 任何人都可以知道发生了什么吗?
By user, I assume you mean an authenticated user? 按用户,我假设您是指经过身份验证的用户? If that is the case, you have to map connections to users first. 如果是这种情况,则必须先将连接映射到用户。 For instance, a user can have 2 or more signalr connections. 例如,用户可以具有2个或更多个信号器连接。 So the first step is mapping users to connections, then you can send a message to the user and all his/her connected agents will receive it. 因此,第一步是将用户映射到连接,然后您可以向用户发送消息,并且他/她所有连接的代理将接收它。
There are several ways to map connections to users, the guide is here: http://www.google.co.uk/url?q=http://www.asp.net/signalr/overview/guide-to-the-api/mapping-users-to-connections&sa=U&ei=Tjj-VJuPMsPBOZGGgYgH&ved=0CAsQFjAA&usg=AFQjCNFXoGJOm3mzenAJbz46TUq-Lx2bvA 有几种方法可以将连接映射到用户,指南在这里: 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
Although this post is already old: I had a similar issue yesterday and it took me hours! 虽然这篇文章已经过时了:昨天我遇到了类似的问题,花了我几个小时! I had the connectionIds but no client received a notification. 我有connectionIds但没有客户收到通知。 Context.Clients.All.aMethod(...)
worked fine, but Context.Clients.Client(theid).aMethod(...)
did not work. Context.Clients.All.aMethod(...)
运行正常,但Context.Clients.Client(theid).aMethod(...)
不起作用。 I finally realized that I stored the connection-ids in the database as an uniqueIdentifier and MS SQL converted the uniqueIdentifier values to uppercase and therefore the connectionids were not valid any more. 我终于意识到我将connection-id作为uniqueIdentifier存储在数据库中,MS SQL将uniqueIdentifier值转换为大写,因此连接不再有效。 I converted the connectinIds to lowercase before publishing to my connected clients and then it worked...maybe you experience a similar problem. 我将connectinIds转换为小写,然后发布到我的连接客户端然后它工作了......也许你遇到了类似的问题。 But your post with an invalid connectionid because of blanks helped my finding the problem. 但是由于空白而你的连接无效的帖子帮我发现了问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.