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

查看:1843
本文介绍了SignalR - 使用(IUserIdProvider)*新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的声明草案,这个bug 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?

更多信息:<一href=\"http://www.asp.net/signalr/overview/signalr-20/hubs-api/mapping-users-to-connections#IUserIdProvider\">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.

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

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();
    }
}

现在,您可以使用将消息发送到特定的用户他的用户id 如文档中提到的,如:

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)*新2.0.0发送消息给特定用户*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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