gpt4 book ai didi

azure - Azure 应用程序服务中的 Signalr 不适用于 WebSockets

转载 作者:行者123 更新时间:2023-12-03 00:38:12 25 4
gpt4 key购买 nike

我有一个客户端(角度)应用程序服务和一个 api(net core 3.1)应用程序服务,我试图通过 signalr 相互通信。它在带有 websocket 的 localhost 上运行良好,但在 azure 应用程序服务上,它仅适用于长轮询传输模式,即使我在配置中启用了 websocket。协商返回 200 以及 connectionId 和所有三个可用传输。但是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.

我尝试查看大量资源和文档来解决这个问题,因此非常感谢您的帮助。这是我的配置:

在startup.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);
});

最佳答案

正如 @Prolog 所提到的,只需在应用程序服务配置中将 WebSockets 开关打开即可。这样就可以了。

Websocket configuration

关于azure - Azure 应用程序服务中的 Signalr 不适用于 WebSockets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66805734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com