简体   繁体   English

如何使用 aspnet core SignalR 向用户发送私人消息?

[英]How can I send a private message to a user with aspnet core SignalR?

I created a project x and I can't see where the error is.我创建了一个项目 x,但我看不到错误在哪里。 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.在我的真实项目中,客户端的上下文中有一个名为 connectionToken 的功能。 How can I get the connectionToken value or why is it there?如何获取 connectionToken 值或为什么存在?

**I use Redis backplane in my real project Example project link: https://github.com/fdevGit/SignalRExample **我在实际项目中使用 Redis 背板 示例项目链接: https://github.com/fdevGit/SignalRExample

From the code you provide, use Clients.User() not Clients.Client() .从您提供的代码中,使用Clients.User()而不是Clients.Client()

Clients.User(...) takes a user identifier. Clients.User(...)采用用户标识符。 Your parameter is connectionId which is a different concept and used with Clients.Client(...) .您的参数是 connectionId ,这是一个不同的概念,与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 .关于callback method的名称还有一个建议。 Return different method name in SendAsync() .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 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测试截图

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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