1

I created a project x and I can't see where the error is. Everything works but the private message doesn't work. The link below is my sample project. In my real project, there is a feature called connectionToken in context on the client. How can I get the connectionToken value or why is it there?

**I use Redis backplane in my real project Example project link: https://github.com/fdevGit/SignalRExample

noja
  • 82
  • 9

1 Answers1

0

From the code you provide, use Clients.User() not Clients.Client().

Clients.User(...) takes a user identifier. Your parameter is connectionId which is a different concept and used with Clients.Client(...).

    public async Task SendMessageToUser(string user, string targetConnectionId, string message)
    {
        Console.WriteLine($"{Context.ConnectionId}-{targetConnectionId}-{message}");
        
        //await Clients.User(...)
        await Clients.Client(targetConnectionId).SendAsync("ReceiveMessageToUser", user, targetConnectionId, message);
    }






There is one more suggestion about the name of callback method. Return different method name in SendAsync().

   public async Task SendMessageToMe(string user, string message)
    {
        Console.WriteLine($"{Context.ConnectionId}-Caller-{message}");
        await Clients.Caller.SendAsync("ReceiveMessageToMe", user, message);
    }
    public async Task SendMessageToUser(string user, string targetConnectionId, string message)
    {
        Console.WriteLine($"{Context.ConnectionId}-{targetConnectionId}-{message}");
        await Clients.Client(targetConnectionId).SendAsync("ReceiveMessageToUser", user, targetConnectionId, message);
    }

Same change in char.js

connection.on("ReceiveMessageToMe", function (user, message) {
    var msg = message.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
    var encodedMsg = user + " says " + msg;
    var li = document.createElement("li");
    li.textContent = encodedMsg;
    document.getElementById("messagesList").appendChild(li);
});

connection.on("ReceiveMessageToUser", function (user, targetConnectionId, message) {
    var msg = message.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
    var encodedMsg = user + " says " + msg + " by " + targetConnectionId;
    var li = document.createElement("li");
    li.textContent = encodedMsg;
    document.getElementById("messagesList").appendChild(li);
});

Screenshots of Test

enter image description here

Michael Wang
  • 3,782
  • 1
  • 5
  • 15
  • In my real project, there is a feature called connectionToken in context on the client. How can I get the connectionToken value or why is it there? – noja Aug 14 '20 at 06:06
  • Connection token is an encrypted string containing the connection id and, if available, user name. It needs to be sent with each http request sent by the client to the server. If a server receives a request without the connection token or if it cannot decrypt the connection token it will reject the request. To read more on connection token and how it works take a look at this article. https://learn.microsoft.com/en-us/aspnet/signalr/overview/security/introduction-to-security#connection-token – Michael Wang Aug 14 '20 at 07:20
  • SignalR passes the connection token as a query string value, instead of as a cookie. Storing the connection token in a cookie is unsafe because the browser can inadvertently forward the connection token when malicious code is encountered. Also, passing the connection token in the query string prevents the connection token from persisting beyond the current connection. – Michael Wang Aug 14 '20 at 07:22
  • Here is a link about how to get the [token]. https://github.com/SignalR/SignalR/issues/3415 – Michael Wang Aug 14 '20 at 07:28