Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ public class HubConnectionBuilder : IHubConnectionBuilder
/// <inheritdoc />
public IServiceCollection Services { get; }

/// <summary>
/// Gets or sets the server timeout interval for the connection.
/// </summary>
/// <remarks>
/// The client times out if it hasn't heard from the server for `this` long.
/// </remarks>
public TimeSpan? ServerTimeout { get; set; }

/// <summary>
/// Gets or sets the interval at which the client sends ping messages.
/// </summary>
/// <remarks>
/// Sending any message resets the timer to the start of the interval.
/// </remarks>
public TimeSpan? KeepAliveInterval { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="HubConnectionBuilder"/> class.
/// </summary>
Expand Down Expand Up @@ -52,7 +68,19 @@ public HubConnection Build()
var endPoint = serviceProvider.GetService<EndPoint>() ??
throw new InvalidOperationException($"Cannot create {nameof(HubConnection)} instance. An {nameof(EndPoint)} was not configured.");

return serviceProvider.GetRequiredService<HubConnection>();
var hubConnection = serviceProvider.GetRequiredService<HubConnection>();

if (ServerTimeout.HasValue)
{
hubConnection.ServerTimeout = ServerTimeout.Value;
}

if (KeepAliveInterval.HasValue)
{
hubConnection.KeepAliveInterval = KeepAliveInterval.Value;
}

return hubConnection;
}

// Prevents from being displayed in intellisense
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#nullable enable
Microsoft.AspNetCore.SignalR.Client.HubConnectionBuilder.KeepAliveInterval.get -> System.TimeSpan?
Microsoft.AspNetCore.SignalR.Client.HubConnectionBuilder.KeepAliveInterval.set -> void
Microsoft.AspNetCore.SignalR.Client.HubConnectionBuilder.ServerTimeout.get -> System.TimeSpan?
Microsoft.AspNetCore.SignalR.Client.HubConnectionBuilder.ServerTimeout.set -> void
static Microsoft.AspNetCore.SignalR.Client.HubConnectionExtensions.On<T1, T2, T3, T4, T5, T6, T7, T8, TResult>(this Microsoft.AspNetCore.SignalR.Client.HubConnection! hubConnection, string! methodName, System.Func<T1, T2, T3, T4, T5, T6, T7, T8, System.Threading.Tasks.Task<TResult>!>! handler) -> System.IDisposable!
static Microsoft.AspNetCore.SignalR.Client.HubConnectionExtensions.On<T1, T2, T3, T4, T5, T6, T7, T8, TResult>(this Microsoft.AspNetCore.SignalR.Client.HubConnection! hubConnection, string! methodName, System.Func<T1, T2, T3, T4, T5, T6, T7, T8, TResult>! handler) -> System.IDisposable!
static Microsoft.AspNetCore.SignalR.Client.HubConnectionExtensions.On<T1, T2, T3, T4, T5, T6, T7, TResult>(this Microsoft.AspNetCore.SignalR.Client.HubConnection! hubConnection, string! methodName, System.Func<T1, T2, T3, T4, T5, T6, T7, System.Threading.Tasks.Task<TResult>!>! handler) -> System.IDisposable!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,32 @@ public void AddMessagePackProtocolSetsHubProtocolToMsgPack()

Assert.IsType<MessagePackHubProtocol>(serviceProvider.GetService<IHubProtocol>());
}

[Fact]
public void CanSetServerTimeout()
{
var serverTimeout = TimeSpan.FromMinutes(1);
var builder = new HubConnectionBuilder();
builder.Services.AddSingleton<IConnectionFactory>(new HttpConnectionFactory(Options.Create(new HttpConnectionOptions()), NullLoggerFactory.Instance));
builder.WithUrl("http://example.com");
builder.ServerTimeout = serverTimeout;

var connection = builder.Build();

Assert.Equal(builder.ServerTimeout, connection.ServerTimeout);
}

[Fact]
public void CanSetKeepAliveInterval()
{
var keepAliveInterval = TimeSpan.FromMinutes(2);
var builder = new HubConnectionBuilder();
builder.Services.AddSingleton<IConnectionFactory>(new HttpConnectionFactory(Options.Create(new HttpConnectionOptions()), NullLoggerFactory.Instance));
builder.WithUrl("http://example.com");
builder.KeepAliveInterval = keepAliveInterval;

var connection = builder.Build();

Assert.Equal(builder.KeepAliveInterval, connection.KeepAliveInterval);
}
}
28 changes: 27 additions & 1 deletion src/SignalR/clients/ts/signalr/src/HubConnectionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ export class HubConnectionBuilder {
/** @internal */
public logger?: ILogger;

/** The server timeout in milliseconds.
*
* If this timeout elapses without receiving any messages from the server, the connection will be terminated with an error.
* The default timeout value is 30,000 milliseconds (30 seconds).
*/
public serverTimeoutInMilliseconds?: number;

/** Default interval at which to ping the server.
*
* The default value is 15,000 milliseconds (15 seconds).
* Allows the server to detect hard disconnects (like when a client unplugs their computer).
* The ping will happen at most as often as the server pings.
* If the server pings every 5 seconds, a value lower than 5 will ping every 5 seconds.
*/
public keepAliveIntervalInMilliseconds?: number;

/** If defined, this indicates the client should automatically attempt to reconnect if the connection is lost. */
/** @internal */
public reconnectPolicy?: IRetryPolicy;
Expand Down Expand Up @@ -204,11 +220,21 @@ export class HubConnectionBuilder {
}
const connection = new HttpConnection(this.url, httpConnectionOptions);

return HubConnection.create(
const hubConnection = HubConnection.create(
connection,
this.logger || NullLogger.instance,
this.protocol || new JsonHubProtocol(),
this.reconnectPolicy);

if (this.serverTimeoutInMilliseconds !== undefined) {
hubConnection.serverTimeoutInMilliseconds = this.serverTimeoutInMilliseconds;
}

if (this.keepAliveIntervalInMilliseconds !== undefined) {
hubConnection.keepAliveIntervalInMilliseconds = this.keepAliveIntervalInMilliseconds;
}

return hubConnection;
}
}

Expand Down