我们有一个Flutter Dart Client应用程序,该应用程序必须与Service Fabric Microservice集成。
客户端代码无法与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中运行的应用程序的服务器代码:
Startup.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();
}
}
}
以下是集线器类:
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
}