如何在我的角度项目上为 signalR 3.0 设置自定义 withAutomaticReconnect

Den*_*fer 4 signalr angular

我正在尝试自定义 SignalR 以尝试在 60 秒内以 3 秒的间隔重新连接

根据文档, withAutomaticReconnect 似乎接受一个实现 IRetryPolicy 接口的对象,该对象有一个名为 nextRetryDelayInMilliseconds 的方法。

这是我需要帮助的地方,但我不知道该怎么做。(打字稿中相当新)我尝试了以下示例,但在 elapsedMilliseconds 上出现代码错误:

类型“number”上不存在属性“elapsedMilliseconds”

https://learn.microsoft.com/en-us/aspnet/core/signalr/javascript-client?view=aspnetcore-3.0#reconnect-clients

const connection = new signalR.HubConnectionBuilder()    
.withAutomaticReconnect({
    nextRetryDelayInMilliseconds: retryContext => {
        if (retryContext.elapsedMilliseconds < 60000) {               
            return 3000;
        } else {
            return null;
        }
    }
})
.build();
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激

Ste*_* CO 5

不确定OP发生了什么,因为我能够按原样复制/粘贴他们的代码并且它对我有用。这是另一个使用像 OP 这样的 lambda 函数的示例,它设置立即尝试的策略,然后在 3 秒后再次尝试,然后在 10 秒后,然后永远每 60 秒尝试一次。

const retryTimes = [0, 3000, 10000, 60000];

const connection = new HubConnectionBuilder()
  .configureLogging(LogLevel.Debug)
  .withAutomaticReconnect({
    nextRetryDelayInMilliseconds: context => {
      const index = context.previousRetryCount < retryTimes.length ? context.previousRetryCount : retryTimes.length - 1;
      return retryTimes[index];
    }
})
.build();
Run Code Online (Sandbox Code Playgroud)