问题描述
我正在尝试使用Ocelot作为代理连接到SignalR集线器。 SignalR插入了网关通过websockets流量传递的微服务。通过HTTP请求进行的协商已成功执行,但是通过websocket的进一步通信似乎丢失了。我不知道发生了什么,特别是当在另一个环境上使用Azure SignalR时,具有相同配置的通信可以完美地工作。下面,我介绍我的网关配置:
ocelot.json
{
"DownstreamPathTemplate": "/{anyHub}/negotiate","DownstreamScheme": "http","DownstreamHostAndPorts": [
{
"Host": "communication","Port": 80
}
],"UpstreamHttpMethod": [ "POST" ],"UpstreamPathTemplate": "/{anyHub}/negotiate","ReRouteIsCaseSensitive": false,"AuthenticationOptions": {
"AuthenticationProviderKey": "Bearer","AllowedScopes": []
},"DelegatingHandlers": [
"IdentityInQuery"
]
},{
"DownstreamPathTemplate": "/{anyHub}","DownstreamScheme": "ws","UpstreamPathTemplate": "/{anyHub}","UpstreamHttpMethod": [ "GET","POST","PUT","DELETE","OPTIONS" ]
},
网关的Program.cs的一部分
.Configure(async app =>
{
await app
.UseCors(cors =>
{
cors.AllowAnyHeader()
.AllowAnyMethod()
.SetIsOriginAllowed(x => true)
.AllowCredentials();
}).UseOcelot();
if (turnOnWebsockets)
app.UseWebSockets();
特定的微服务集合扩展:
public static ISignalRBuilder AddSignalRConfiguration(this IServiceCollection services,bool isDevelopment)
{
var newServices = services.AddSignalR();
if (!isDevelopment) newServices.AddAzureSignalR(Config.AzureSignalROptions.ConnectionString);
return newServices;
}
public static IServiceCollection AddSignalRCors(this IServiceCollection services)
{
services.AddCors(options => options.AddPolicy("CorsPolicy",builder =>
{
builder
.AllowAnyHeader()
.AllowAnyMethod()
.SetIsOriginAllowed(x => true)
.AllowCredentials();
}));
return services;
}
特定于微服务的IApplicationBuilder扩展的一部分:
public static IApplicationBuilder AddSignalR(this IApplicationBuilder app,bool isDevelopment)
{
app.UseRouting()
.UseCors("CorsPolicy");
if (isDevelopment)
{
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<UserHub>("/userHub");
endpoints.MapHub<ConversationHub>("/conversationHub");
endpoints.MapHub<DiscussionHub>("/discussionHub");
});
}
....
return app;
}
如何将Websocket与Ocelot和SignalR一起使用?网关当前能够与SignalR通信的唯一传输方法是长轮询,但对我而言,这并不完全令人满意。预先感谢您的帮助!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)