azure - 天蓝色应用程序服务中的 Signalr 不适用于 websocket
问题描述
我有一个客户端(角度)应用程序服务和一个 api(net core 3.1)应用程序服务,我试图通过信号器相互交谈。它在带有 websocket 的 localhost 上运行良好,但在 azure app 服务上它仅适用于 longpolling 传输模式,即使我在配置中启用了 websocket。协商返回 200 以及 connectionId 和所有三个 availableTransport。但是 wss 请求返回 404 如下所示。
WebSocket connection to 'wss://xxxx.azurewebsites.net/notifications?id=ZI0-a_p5G6YziWhCxxc7Kg&access_token=eyJhbGc.. failed: Error during WebSocket handshake: Unexpected response code: 404
Utils.js:218 [2021-03-25T18:21:13.045Z] Error: Failed to start the transport 'WebSockets': Error: There was an error with the transport.
我已经尝试查看大量资源和文档来解决这个问题,因此感谢您提供任何帮助。这是我的配置:
在启动.cs
options.AddPolicy(MyAllowSpecificOrigins,
builder =>
{
builder.WithOrigins("https://xxxxx-dev.azurewebsites.net").AllowAnyMethod().AllowAnyHeader().AllowCredentials();
builder.WithOrigins("http://localhost:4200").AllowAnyMethod().AllowAnyHeader().AllowCredentials();
});
....
services.AddControllers();
services.AddSignalR();
....
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHub<NotificationHub>("/notifications");
});
在客户端
import { HubConnectionBuilder } from '@microsoft/signalr';
....
this.hubConnection = new HubConnectionBuilder()
.withUrl("https://xxxx-dev.net/notifications",
{
accessTokenFactory: () => this.authService.getToken(),
skipNegotiation: false,
transport: signalR.HttpTransportType.None
}).configureLogging(signalR.LogLevel.Information).build();
this.hubConnection.on("test", (msg) => {
console.log("pushed data:", msg)
});
this.hubConnection.start()
.then(() => {
console.log("Connected")
})
.catch(err => {
console.error(err);
});
解决方案
推荐阅读
- java - Swagger (OpenAPI 3.0) 仅在 Post 上隐藏字段,而不是在 Put 上
- javascript - 传递变量以获取 API
- angular - Angular FE 和 fastAPI GCP VM 上的 CORS 错误
- amazon-web-services - 将特定日志从 AWS Cloudtrail 移动到 S3
- python - 如何在 Dash 中动态更新下拉列表?
- azure-data-factory - 如何使用 Azure 数据流过滤具有多个案例的单列记录
- node.js - 类型错误:#
不是函数 - vaadin - 如何在vaadin路由器中制作带有参数的动态路由器
- c# - 无法与 ad mob 统一加载插页式广告
- java - android.support.v7.widget item.SearchView长按事件