简体   繁体   English

向特定用户发送 Signalr 消息

[英]Send Signalr message to specific user

In my project I have SignalR set up where messages are getting sent to every user that connects to my site.在我的项目中,我设置了 SignalR,消息将发送到连接到我站点的每个用户。 I'm in the process of changing this so my messages only go to those users that generated them.我正在对此进行更改,因此我的消息仅 go 发送给生成它们的用户。 However, I am unable to get my users identity and I was hoping someone would be able to tell me what I've done wrong/missing.但是,我无法获得我的用户身份,我希望有人能够告诉我我做错了什么/遗漏了什么。

In my StartUp class I have the following lines inside of ConfigureServices :在我的StartUp class 中,我在ConfigureServices中有以下几行:

services.AddSignalR();
services.AddSingleton<IUserIdProvider, NameUserIdProvider>();
services.AddMvc(); 
services.AddAuthentication();

Then in my Configure I have the following lines:然后在我的Configure中有以下几行:

app.UseAuthentication();
app.UseAuthorization();
app.UseWebSockets();
app.UseEndpoints(endpoints =>
{
  endpoints.MapHub<UserErrorHub>("/UserErrorHub", options =>
  {
    options.Transports =
       HttpTransportType.WebSockets |
       HttpTransportType.LongPolling;
  });
  endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
});

Next, my view has the following connection string set up in a <script> block:接下来,我的视图在<script>块中设置了以下连接字符串:

 var url = "@Url.Content("~/UserErrorHub")";

    var connection = new signalR.HubConnectionBuilder()
        .withUrl(url, { transport: signalR.HttpTransportType.WebSockets | signalR.HttpTransportType.LongPolling}, options =>
        {
            options.UseDefaultCredentials = true;     
        }
        .configureLogging(signalR.LogLevel.Information)
        .build();

And finally I have the following classes in my hub:最后,我的中心有以下课程:

public class UserErrorHub : Hub
{
    public async override Task OnConnectedAsync()
    {
        string name;
        var user = Context.User;
        if (user.Identity.IsAuthenticated)
        {
            name = user.Identity.Name;
        }
        else
        {
            name = "anonymous";
        }
        await Clients.All.SendAsync("Message",  "hello there!" + name);
    }

    public async Task Message( string message)
    {
        await Clients.All.SendAsync("ReceiveMessage", message);
    }
}

public class NameUserIdProvider : IUserIdProvider
{
    public string GetUserId(HubConnectionContext connection)
    {
        return connection.User?.Identity?.Name;
    }
}

When I run my project, I am unable to get my Username to appear when I connect, instead all "anonymous" appearing which isn't what I want.当我运行我的项目时,我无法在连接时显示我的用户名,而是显示所有“匿名”,这不是我想要的。 Could someone please tell me what it is I'm doing wrong?有人可以告诉我我做错了什么吗?

For my scenario I had to remove the singalR Endpoints and use the older depriecated way对于我的场景,我不得不删除 singalR 端点并使用旧的 depriecated 方式

        app.UseSignalR(builder =>
        {
            builder.MapHub<UserErrorHub >("/userErrorHub");
        });

Don't forget the Authorize attribute on the hub不要忘记集线器上的授权属性

       [Authorize]

In my project I have SignalR set up where messages are getting sent to every user that connects to my site.在我的项目中,我设置了 SignalR,其中将消息发送给连接到我网站的每个用户。 I'm in the process of changing this so my messages only go to those users that generated them.我正在更改此设置,因此我的消息只会发送给生成它们的用户。 However, I am unable to get my users identity and I was hoping someone would be able to tell me what I've done wrong/missing.但是,我无法获得我的用户身份,我希望有人能够告诉我我做错了什么/遗漏了什么。

In my StartUp class I have the following lines inside of ConfigureServices :在我的StartUp类中,我在ConfigureServices有以下几行:

services.AddSignalR();
services.AddSingleton<IUserIdProvider, NameUserIdProvider>();
services.AddMvc(); 
services.AddAuthentication();

Then in my Configure I have the following lines:然后在我的Configure我有以下几行:

app.UseAuthentication();
app.UseAuthorization();
app.UseWebSockets();
app.UseEndpoints(endpoints =>
{
  endpoints.MapHub<UserErrorHub>("/UserErrorHub", options =>
  {
    options.Transports =
       HttpTransportType.WebSockets |
       HttpTransportType.LongPolling;
  });
  endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
});

Next, my view has the following connection string set up in a <script> block:接下来,我的视图在<script>块中设置了以下连接字符串:

 var url = "@Url.Content("~/UserErrorHub")";

    var connection = new signalR.HubConnectionBuilder()
        .withUrl(url, { transport: signalR.HttpTransportType.WebSockets | signalR.HttpTransportType.LongPolling}, options =>
        {
            options.UseDefaultCredentials = true;     
        }
        .configureLogging(signalR.LogLevel.Information)
        .build();

And finally I have the following classes in my hub:最后,我的集线器中有以下课程:

public class UserErrorHub : Hub
{
    public async override Task OnConnectedAsync()
    {
        string name;
        var user = Context.User;
        if (user.Identity.IsAuthenticated)
        {
            name = user.Identity.Name;
        }
        else
        {
            name = "anonymous";
        }
        await Clients.All.SendAsync("Message",  "hello there!" + name);
    }

    public async Task Message( string message)
    {
        await Clients.All.SendAsync("ReceiveMessage", message);
    }
}

public class NameUserIdProvider : IUserIdProvider
{
    public string GetUserId(HubConnectionContext connection)
    {
        return connection.User?.Identity?.Name;
    }
}

When I run my project, I am unable to get my Username to appear when I connect, instead all "anonymous" appearing which isn't what I want.当我运行我的项目时,我无法在连接时显示我的用户名,而是出现所有“匿名”,这不是我想要的。 Could someone please tell me what it is I'm doing wrong?有人可以告诉我我做错了什么吗?

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

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