How can I send a message to a specific user with a signal

I have some problem with signalR, I can not send a message to a specific user from the hub.

I want to do like this:

public void Send(string userToId, string userForId, string message) { IHubContext context = GlobalHost.ConnectionManager.GetHubContext<ChatHubs>(); //userForId - it is Session["UserId"] context.Clients.User(userForId).updateMessages(message); } 

I already read this topic: http://www.asp.net/signalr/overview/guide-to-the-api/mapping-users-to-connections , but this is not clear to me because I do not have this var name = Context.User.Identity.Name; I have User Information in the Session variable and the second when I get connectionId as follows: var myClientId = $.connection.hub.id; connectionId - changes when the page is refreshed or when another menu in my project is clicked.

I have a few questions: 1) Can I send a message to a specific user without connectionId (use only the ["UserId"] session)? 2) Overall, how can I easily send one-to-one messages using SignalR?

PS I am running ASP.NET MVC (C #)

+6
source share
2 answers

You can send broadcast messages to all users without a connection identifier. You just need to assign a unique identifier for each user and send it as message parameters.

SignalR Gives a unique identifier to each client as a connection identifier. Either you can use this connection identifier, or you can assign a unique identifier to the client when creating the client and use it as the connection identifier. It is up to you what you want to use.

Edit

Just update your method in the hub class file .....

  public void Send(string name, string message, string connectionid) { // Call the addNewMessageToPage method to update clients. Clients.All.addNewMessageToPage(name, message, connectionid); } 

And on the client side, you can update the add code After including SignalR files: -

  var chat = $.connection.chatHub; chat.client.addNewMessageToPage = function (name, message, connectionid) { if (connectionid == $('#connection').val()) { // Do What You want Here... }; }; // Get the user name and store it to prepend to messages. $('#displayname').val(prompt('Enter your name:', '')); $('#connection').val(prompt('Enter your ID:', '')); // Set initial focus to message input box. $('#message').focus(); // Start the connection. $.connection.hub.start().done(function () { $('#sendmessage').click(function () { // Call the Send method on the hub. chat.server.send($('#displayname').val(), $('#message').val(), $('#connection').val()); // Clear text box and reset focus for next comment. $('#message').val('').focus(); }); }); 

Hope this helps ...

+7
source

If you want to send a message to a specific user in SignalR, the easiest way is to use form authentication. You can also use your own session with form authentication. Immediately after creating the session code, enter this code.

 FormsAuthentication.SetAuthCookie(username.Trim(), false); 

Then in signalR you can use this line to send a message to this user:

 var username = Context.User.Identity.Name; context.Clients.User(username).updateMessages(message); 

EDIT:

For your question, pass the username to this method (receiver username) and press the message to this user. Then you do not need to specify userForId, because you already have a sender username with the name "var username = Context.User.Identity.Name;". Also, this method will simply click on the recipient username method. If you also want to receive the message to the sender, you will need the user "Caller", and you need to write a new function to receive caller messages in javascript. I hope this is clear to you.

 public void Send(string username, string message) { context.Clients.Caller.updateMessagesCaller(message); context.Clients.User(username).updateMessages(message); } 

This message will only indicate the username. And my recommendation uses the implementation of FormAuthentication throughout the project. Thanks.

+3
source

Source: https://habr.com/ru/post/982974/


All Articles