首页 > 解决方案 > 天蓝色应用程序服务中的 Signalr 不适用于 websocket

问题描述

我有一个客户端(角度)应用程序服务和一个 api(net core 3.1)应用程序服务,我试图通过信号器相互交谈。它在带有 websocket 的 localhost 上运行良好,但在 azure app 服务上它仅适用于 longpolling 传输模式,即使我在配置中启用了 websocket。协商返回 200 以及 connectionId 和所有三个 availableTransport。但是 wss 请求返回 404 如下所示。

WebSocket connection to 'wss://xxxx.azurewebsites.net/notifications?id=ZI0-a_p5G6YziWhCxxc7Kg&access_token=eyJhbGc.. failed: Error during WebSocket handshake: Unexpected response code: 404 
Utils.js:218 [2021-03-25T18:21:13.045Z] Error: Failed to start the transport 'WebSockets': Error: There was an error with the transport.

我已经尝试查看大量资源和文档来解决这个问题,因此感谢您提供任何帮助。这是我的配置:

在启动.cs

options.AddPolicy(MyAllowSpecificOrigins,
                    builder =>
                    {
                        builder.WithOrigins("https://xxxxx-dev.azurewebsites.net").AllowAnyMethod().AllowAnyHeader().AllowCredentials();
                        builder.WithOrigins("http://localhost:4200").AllowAnyMethod().AllowAnyHeader().AllowCredentials();

                    });

....

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

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

在客户端

import { HubConnectionBuilder } from '@microsoft/signalr';

....
    this.hubConnection = new HubConnectionBuilder()
    .withUrl("https://xxxx-dev.net/notifications",
      {
        accessTokenFactory: () => this.authService.getToken(),
        skipNegotiation: false,
        transport: signalR.HttpTransportType.None
     

      }).configureLogging(signalR.LogLevel.Information).build();
    
    this.hubConnection.on("test", (msg) => {
      console.log("pushed data:", msg)
    });

      this.hubConnection.start()
      .then(() => {
        console.log("Connected")
      })
      .catch(err => {
        console.error(err);
      });

标签: azure.net-coresignalrasp.net-core-signalrazure-signalr

解决方案


推荐阅读