Hah*_*aha 1 azure signalr .net-core asp.net-core-signalr azure-signalr
我有一个客户端(角度)应用程序服务和一个 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.
Run Code Online (Sandbox Code Playgroud)
我尝试查看大量资源和文档来解决这个问题,因此我们将不胜感激。这是我的配置:
在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");
});
Run Code Online (Sandbox Code Playgroud)
在客户端
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);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2381 次 |
| 最近记录: |