使用 Signalr 在 .net core 3.1 和 Angular 之间连接期间出现 Websocket 错误

Websocket error during connection between .net core 3.1 and Angular with Signalr

我创建了一个 .net 核心 Web api 项目并尝试使用 SignalR 并连接到 Angular 应用程序。

我在与 Angular 建立连接时遇到错误。

WebSocketTransport.js:85 WebSocket connection to 'wss://localhost:44363/api/message' failed: Error during 
WebSocket handshake: Unexpected response code: 200

我的startup.cs

namespace SignalR_server
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy", builder => builder
                .WithOrigins("http://localhost:4200")
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowCredentials());
            });

            services.AddSignalR();
            services.AddControllers();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseCors("CorsPolicy");


            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapHub<MessageHub>("/message");
            });
        }                                                                                           
    }
}

和MessageController.cs

public class MessageController : ControllerBase
    {
        private IHubContext<MessageHub> _myHub; 

        public MessageController(IHubContext<MessageHub> hub)
        {
            _myHub = hub;
        }

        [HttpGet]
        public Message Get()
        {
            Message msg = new Message();
            msg.Id = 1;
            msg.Name = "name";
            msg.Msg = "This is a test message";
            _myHub.Clients.All.SendAsync("transfermessage", msg);
            return msg;
        }

在我的客户端连接中,我将 skipNegotiation 设置为 true

  public startConnection = () => {
    this.hubConnection = new signalR.HubConnectionBuilder()
                            .withUrl('https://localhost:44363/api/message',{
                              skipNegotiation: true,
                              transport: signalR.HttpTransportType.WebSockets
                            })
                            .build();

我错过了什么?

当您使用代码 endpoints.MapHub<MessageHub>("/message") 时,您将“/message”设置为您的完整路径。因此,您的信号器中心端点将是 'https://localhost:44363/message',而不是 'https://localhost:44363/api/message'。

以后当您看到“预期 200”时,请务必检查浏览器开发人员工具的网络选项卡。此选项卡将向您显示您遇到的真正错误。在这种情况下,我假设您会收到 404。