首页 > 解决方案 > 使用 Azure Service Fabric 的 Signal R 握手失败

问题描述

我们有一个必须与 Service Fabric 微服务集成的 Flutter Dart 客户端应用程序。

客户端代码无法与 Signal R 建立连接,在进一步调试客户端代码时,我们看到握手请求失败。

下面是客户端代码:

 Future<void> openChatConnection() async {
    if (_hubConnection == null) {

       //final logger = attachToLogger();
      _hubConnection = HubConnectionBuilder().withUrl(_serverUrl).build();
      _hubConnection.onclose((error) => connectionIsOpen = false);

      _hubConnection.on("OnMessage", _handleIncommingChatMessage);


    }

    if (_hubConnection.state != HubConnectionState.Connected) {
      await _hubConnection.start();
      connectionIsOpen = true;
    }

下面是在 Service Fabric 中运行的应用程序的服务器代码:

启动.cs

public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            IConfigurationBuilder builder = new ConfigurationBuilder();
                // .SetBasePath(env.ContentRootPath)
                // // .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                // // .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                // .AddEnvironmentVariables();
            this.Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddMvc();
            services.AddSignalR();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            // if (env.IsDevelopment())
            // {
            //     app.UseDeveloperExceptionPage();
            //     app.UseBrowserLink();
            // }
            // else
            // {
            //     app.UseExceptionHandler("/Home/Error");
            // }

            // app.UseStaticFiles();
             app.UseSignalR(routes =>
            {
        routes.MapHub<NotificationClient>("/Chat");
          });

            app.UseMvc(
                routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}");
                });
            app.UseCors();
        }
    }
}

下面是 Hub 类:

public class NotificationClient : Hub
  {

    #region Consts, Fields, Properties, Events

    #endregion

    #region Methods

    public void Send(string name, string message)
    {
      // Call the "OnMessage" method to update clients.

      Clients.All.SendCoreAsync("OnMessage", new object[]{name, message});

    }
    #endregion
  }

标签: azureflutterdartsignalrazure-service-fabric

解决方案


推荐阅读