azure - 使用 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
}
解决方案
推荐阅读
- kubernetes - 使用`status.hostIP`作为kubernetes中我的env变量的参考字段时的空变量
- multithreading - 在独立应用程序中成功完成进程后如何杀死 Apache Camel 父线程?
- android - 运行“splash”时出错:必须导出活动或包含意图过滤器
- node.js - node.js fs Readstream 事件处理程序从未在 Node 12.3.1 中调用
- python - 根据 Python 中的字典值分离输入数据
- excel - AdvancedFilter 创建一个命名范围
- r - sessionInfo()在不同平台上的不一致
- xamarin.forms - Apk 大小变小并损坏,Github 操作没有任何变化
- javascript - 如果 LI 标签有一个 active 类,它将转到最后一个位置
- webpack - 使用 ScalaJSBundlerPlugin (webpack bundler) 时无法从 Javascript 访问 JSExport