SignalR - 使用 (IUserIdProvider) *NEW 2.0.0* 向特定用户发送消息 [英] SignalR - Sending a message to a specific user using (IUserIdProvider) *NEW 2.0.0*

查看:26
本文介绍了SignalR - 使用 (IUserIdProvider) *NEW 2.0.0* 向特定用户发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最新版本的 Asp.Net SignalR 中,添加了一种使用IUserIdProvider"接口向特定用户发送消息的新方式.

In the latest version of Asp.Net SignalR, was added a new way of sending a message to a specific user, using the interface "IUserIdProvider".

public interface IUserIdProvider
{
   string GetUserId(IRequest request);
}

public class MyHub : Hub
{
   public void Send(string userId, string message)
   {
      Clients.User(userId).send(message);
   }
}

我的问题是:我怎么知道我要向谁发送消息?这种新方法的解释非常肤浅.并且带有此错误的 SignalR 2.0.0 声明草案无法编译.有没有人实现过这个功能?

My question is: How do I know to whom I am sending my message? The explanation of this new method is very superficial. And the draft Statement of SignalR 2.0.0 with this bug and does not compile. Has anyone implemented this feature?

更多信息:http://www.asp.net/signalr/overview/signalr-20/hubs-api/mapping-users-to-connections#IUserIdProvider

拥抱.

推荐答案

SignalR 为每个连接提供 ConnectionId.要找到哪个连接属于谁(用户),我们需要在连接和用户之间创建一个映射.这取决于您在应用中识别用户的方式.

SignalR provides ConnectionId for each connection. To find which connection belongs to whom (the user), we need to create a mapping between the connection and the user. This depends on how you identify a user in your application.

在 SignalR 2.0 中,这是通过使用内置的 IPrincipal.Identity.Name 完成的,这是在 ASP.NET 身份验证期间设置的登录用户标识符.

In SignalR 2.0, this is done by using the inbuilt IPrincipal.Identity.Name, which is the logged in user identifier as set during the ASP.NET authentication.

但是,您可能需要使用不同的标识符而不是 Identity.Name 来映射与用户的连接.为此,此新提供程序可与您的自定义实现一起使用,以将用户与连接映射.

However, you may need to map the connection with the user using a different identifier instead of using the Identity.Name. For this purpose this new provider can be used with your custom implementation for mapping user with the connection.

让我们假设我们的应用程序使用 userId 来标识每个用户.现在,我们需要向特定用户发送消息.我们有 userIdmessage,但 SignalR 还必须知道我们的 userId 和连接之间的映射.

Lets assume our application uses a userId to identify each user. Now, we need to send message to a specific user. We have userId and message, but SignalR must also know the mapping between our userId and the connection.

为了实现这一点,首先我们需要创建一个实现IUserIdProvider的新类:

To achieve this, first we need to create a new class which implements IUserIdProvider:

public class CustomUserIdProvider : IUserIdProvider
{
     public string GetUserId(IRequest request)
    {
        // your logic to fetch a user identifier goes here.

        // for example:

        var userId = MyCustomUserClass.FindUserId(request.User.Identity.Name);
        return userId.ToString();
    }
}

第二步是告诉 SignalR 使用我们的 CustomUserIdProvider 而不是默认实现.这可以在初始化集线器配置时在 Startup.cs 中完成:

The second step is to tell SignalR to use our CustomUserIdProvider instead of the default implementation. This can be done in the Startup.cs while initializing the hub configuration:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var idProvider = new CustomUserIdProvider();

        GlobalHost.DependencyResolver.Register(typeof(IUserIdProvider), () => idProvider);          

        // Any connection or hub wire up and configuration should go here
        app.MapSignalR();
    }
}

现在,您可以使用文档中提到的 userId 向特定用户发送消息,例如:

Now, you can send message to a specific user using his userId as mentioned in the documentation, like:

public class MyHub : Hub
{
   public void Send(string userId, string message)
   {
      Clients.User(userId).send(message);
   }
}

希望这会有所帮助.

这篇关于SignalR - 使用 (IUserIdProvider) *NEW 2.0.0* 向特定用户发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆