gpt4 book ai didi

.net - Azure Signalr 服务拒绝将连接添加到组

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

我一直在 .NET Core 中开发 API 并在 Angular 中开发前端。在 localhost 中,我让 signalr 在 API 中设置了 Hub:

程序.cs

builder.Services.AddSignalR()
.AddJsonProtocol(options =>
{
options.PayloadSerializerOptions.Converters
.Add(new JsonStringEnumConverter());
});

...

app.MapHub<NotificationHub>("/notifications");

我让前端监听:

  private options: signalR.IHttpConnectionOptions = {
accessTokenFactory: () => {
return this.userService.authToken as string;
},
skipNegotiation: true,
transport: signalR.HttpTransportType.WebSockets
};

private hubConnection: signalR.HubConnection | undefined
public startConnection = () => {
this.hubConnection = new signalR.HubConnectionBuilder()
.withUrl(`${environment.backendApi}/notifications`, this.options)
.configureLogging(signalR.LogLevel.Trace)
.build();
this.hubConnection
.start()
.then(() => console.log("notification connection started"))
.catch(err => console.log('Error wgile start candidate connection: ' + err));
}

...

public addApplicationListener = () => {
this.hubConnection?.on('notifications', (data) => {
console.log(data);
});
}

但是自从将我的后端连接到我的 Azure SignalR 服务后,我可以从两个应用程序获得到 WebSocket 的连接,但是当我尝试将 Angular 连接添加到一个组(通过 Hub 在 API 中)时,我得到了在我的 Azure 服务 ConnectionIsInvalidOnJoiningGroup 中出现警告。因此,当我向群组发送消息时,它提示群组中没有连接。

https://learn.microsoft.com/en-us/azure/azure-signalr/concept-service-mode#choose-the-right-service-mode

此链接似乎表明默认服务是正确使用的服务,如果您要从自托管信号器转移,它应该是直接交换...

最佳答案

@Brennan评论解决了我的问题。

options: signalR.IHttpConnectionOptions中的skipNotification更改为false Angular 中的属性。

private options: signalR.IHttpConnectionOptions = {
accessTokenFactory: () => {
return this.userService.authToken as string;
},
transport: signalR.HttpTransportType.WebSockets
};

然后在我的.NET API Program.cs ,我添加了UseAzureSignalR()方法。所以这个 app.MapHub<NotificationHub>("/notifications");成为:

app.UseAzureSignalR(options =>
{
options.MapHub<NotificationHub>("/notifications");
});

关于.net - Azure Signalr 服务拒绝将连接添加到组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75326055/

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