【问题标题】:.Net Core 3.0 SignalR client connection problem.Net Core 3.0 SignalR 客户端连接问题
【发布时间】:2020-02-06 17:17:02
【问题描述】:

我创建了 2 个项目。第一个项目是 .net 核心 mvc 应用程序与 SignalR 一起使用,第二个项目是客户端。所以在我的服务器正在运行但是当我想从 console.app 连接到 signalR 时出现了一些错误。

这是一个代码。 (客户)

HubConnection conn = new HubConnectionBuilder()
             .WithUrl("http://localhost:5001/userHub")
             .Build();

        conn.StartAsync().ContinueWith(t=>{
            if(t.IsFaulted)
                Console.WriteLine(t.Exception.GetBaseException());
            else
                Console.WriteLine("Connected to Hub");

        }).Wait();  

服务器:

  public class UserHub : Microsoft.AspNetCore.SignalR.Hub
{
    public override Task OnConnectedAsync()
    {
        System.Console.WriteLine($"New Connection {Context.ConnectionId}");

        Clients.All.SendAsync("ReceiveMessage","New Connection Id", Context.ConnectionId);
        return base.OnConnectedAsync();
    }

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

服务器(配置)

public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
        services.AddSignalR();
    }


        app.UseEndpoints(endpoints =>
        {
            endpoints.MapHub<UserHub>("/userHub");
        });

enter image description here

【问题讨论】:

  • 嗨@user3304820,关于这个问题的任何更新?您是否尝试过修改我在帖子中提到的 URL?

标签: asp.net-core signalr.client


【解决方案1】:

System.Net.Http.HttpRequestException:发送请求时出错。

---> System.IO.IOException: 响应提前结束。

我可以通过运行 dotnet run 命令在https://localhost:5001http://localhost:5000 服务时重现与 URL http://localhost:5001/userHub 相同的问题。

如需解决此问题,请将代码修改为.WithUrl("https://localhost:5001/userHub")

SignalR .NET 客户端

Console.WriteLine("Signalr Client Starting...");

HubConnection conn = new HubConnectionBuilder()
    .WithUrl("https://localhost:5001/userHub")
    .Build();

conn.StartAsync().ContinueWith(t => {
    if (t.IsFaulted)
        Console.WriteLine(t.Exception.GetBaseException());
    else
        Console.WriteLine("Connected to Hub");

}).Wait();

测试结果

【讨论】:

  • 这为我整理好了。我只是像在本地 API 调用中一样使用 http,但它不起作用,将其更改为 https 似乎允许连接发生。
猜你喜欢
  • 1970-01-01
  • 2019-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-09
  • 2020-06-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多