如何在角度 8 项目中为 SignalR 配置重新连接逻辑?



我正在使用

"@aspnet/signalr": "^1.1.4",

在 Angular 8 项目中。

不幸的是,文档似乎已过时?

https://learn.microsoft.com/en-us/aspnet/core/signalr/javascript-client?view=aspnetcore-3.1

文档声称有一种方法

withAutomaticReconnect()

用作

const connection = new signalR.HubConnectionBuilder()
.withUrl("/chatHub")
.withAutomaticReconnect()
.build();

但是,当我尝试输入它时,我得到

属性"WithAutomaticReconnect"在类型"HubConnectionBuilder"上不存在

这是一个文档过时的问题吗?还是我误解了配置?

这是我在尝试添加此方法之前的确切代码

private _hubConnection: HubConnection;

this._hubConnection = new HubConnectionBuilder()
.withUrl(this.myAPIUrl + '/myHub', { accessTokenFactory: () => tokenFromCache })
.configureLogging(signalR.LogLevel.Error)
.build();

您引用的文档使用 npm 包"@microsoft/signalr",而您的文档使用 "@aspnet/signalr">

您需要一个更新的版本,我相信它是版本 3,只需在您的终端中运行以下命令,我正在将其与反应前端一起使用。

npm install @microsoft/signalr

也许你可以这样做

this._hubConnection.start().catch(_ => setTimeout(() => {
this._hubConnection.start();
}, 1000));

因此,如果有任何异常,它将把你的客户端重新连接到中心

最新更新