【问题标题】:SignalR between server and client in Docker not workingDocker中服务器和客户端之间的SignalR不起作用
【发布时间】:2022-11-03 20:05:42
【问题描述】:

我有一个与 SignalR 通信的 ASP.NET Web MVC 和 Web API。当我在 localhost 上运行它们时,一切正常,但现在我需要部署它们,并且由于我也有几个 Python grpc 服务,所以我决定使用 docker-compose 来完成。所以我的 Web 应用在 https://fast_web_app:5001 上,而 Web Api 在 https://fast_api:7131 上,在 js 文件中的 Web 应用中,我连接到集线器,如:

const hubConnection = new signalR.HubConnectionBuilder()
    .withUrl("https://fast_api:7131/pageUpdateHub")
    .withAutomaticReconnect()
    .configureLogging(signalR.LogLevel.Trace)
    .build();

我还在 Web Api 中配置了 CORS,如下所示:

        app.UseHsts();
        app.UseHttpsRedirection();
        
        app.UseCors(builder =>
        {
            builder.WithOrigins("https://fast_web_app:5001")
                .AllowAnyHeader()
                .WithMethods("GET", "POST")
                .AllowCredentials();
        });
        
        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapHub<PageUpdateHub>("/pageUpdateHub");
            ...
        }

但我收到一个错误:

[2022-06-04T00:22:50.715Z] Debug: Sending negotiation request: https://fast_api:7131/pageUpdateHub/negotiate?negotiateVersion=1.
POST https://fast_api:7131/pageUpdateHub/negotiate?negotiateVersion=1 net::ERR_NAME_NOT_RESOLVED

我也尝试像这样从 Web App 连接到集线器:

const hubConnection = new signalR.HubConnectionBuilder()
    .withUrl("https://fastml_api:7131/pageUpdateHub", {
        skipNegotiation: true,
        transport: signalR.HttpTransportType.WebSockets
    })
    .withAutomaticReconnect()
    .configureLogging(signalR.LogLevel.Trace)
    .build();

但后来我得到这个错误:

WebSocket connection to 'wss://fast_api:7131/pageUpdateHub' failed: 
Utils.ts:193 [2022-06-04T00:35:18.749Z] Error: Failed to start the connection: Error: WebSocket failed to connect. The connection could not be found on the server, either the endpoint may not be a SignalR endpoint, the connection ID is not present on the server, or there is a proxy blocking WebSockets. If you have multiple servers check that sticky sessions are enabled.

看起来我错过了一些东西,任何想法如何使这项工作?

除了 SignalR Api 和 App 还与 HTTP 通信,一切正常。

Docker-compose 的一部分:

services:
  fast_web_app:
    image: fast_web_app
    ports:
      - "5001:5001"
      - "5002:5002"
    networks:
      - fast
    environment:
      - ASPNETCORE_URLS=https://*:5001;http://*:5002
      - ASPNETCORE_Kestrel__Certificates__Default__Password=***
      - ASPNETCORE_Kestrel__Certificates__Default__Path=/app/Certificates/certificate.pfx
      - ASPNETCORE_HTTPS_PORT=5001
    volumes:
      - /Users/***/Certificates/:/app/Certificates
      - fast_server_storage:/app/wwwroot

  fast_api:
    image: fast_api
    ports:
      - "7131:7131"
      - "7132:7132"
    networks:
      - fast
    environment:
      - ASPNETCORE_URLS=https://*:7131;http://*:7132
      - ASPNETCORE_Kestrel__Certificates__Default__Password=***
      - ASPNETCORE_Kestrel__Certificates__Default__Path=/app/Certificates/certificate.pfx
      - ASPNETCORE_HTTPS_PORT=7131
    volumes:
      - /Users/***/Certificates/:/app/Certificates
      - fast_server_storage:/app/wwwroot

【问题讨论】:

    标签: docker asp.net-core docker-compose signalr


    【解决方案1】:

    最后通过将客户端js代码中的“https://fast:7131/pageUpdateHub”替换为“https://0.0.0.0:7131/pageUpdateHub”解决了这个问题。 科斯政策:

        services.AddCors(options => options.AddPolicy("CorsPolicy",
                builder =>
                {
                    builder
                        .AllowAnyMethod()
                        .AllowAnyHeader()
                        .AllowCredentials()
                        .SetIsOriginAllowed(_ => true);
                }));
            services.AddSignalR();
    
      app.UseCors("CorsPolicy");
            app.UseRouting();
    
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapHub<PageUpdateHub>("/pageUpdateHub");
    

    【讨论】:

      【解决方案2】:

      除了上一个答案。当使用容器名称而不是 IP 时,删除端口就足够了:https://fast/pageUpdateHub

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-13
        • 1970-01-01
        • 1970-01-01
        • 2016-11-19
        • 2015-07-05
        • 2020-06-09
        相关资源
        最近更新 更多