using Microsoft.AspNetCore.SignalR.Client;
/// <summary>
/// Signalr重连策略
/// </summary>
public class RetryPolicy : IRetryPolicy
{
/// <summary>
/// 重连规则:重连次数<50:间隔10s;重试次数<250:间隔30s;重试次数>250:间隔1m
/// </summary>
/// <param name="retryContext"></param>
/// <returns></returns>
public TimeSpan? NextRetryDelay(RetryContext retryContext)
{
var count = retryContext.PreviousRetryCount / 50;
if (count < 1)//重试次数<50,间隔10s
{
return new TimeSpan(0, 0, 10);
}
else if (count < 5)//重试次数<250:间隔30s
{
return new TimeSpan(0, 0, 30);
}
else //重试次数>250:间隔1m
{
return new TimeSpan(0, 1, 0);
}
}
}
}
使用:
var hubConnection = new HubConnectionBuilder()
.WithUrl($"http://localhost/hub").WithAutomaticReconnect(new RetryPolicy()).ConfigureLogging(logging =>
{
logging.SetMinimumLevel(LogLevel.Debug);
})
.Build();