diff --git a/clients/java/signalr/src/test/java/com/microsoft/signalr/sample/Chat.java b/clients/java/signalr/src/test/java/com/microsoft/signalr/sample/Chat.java index 5ea44083c5..2e0990d70a 100644 --- a/clients/java/signalr/src/test/java/com/microsoft/signalr/sample/Chat.java +++ b/clients/java/signalr/src/test/java/com/microsoft/signalr/sample/Chat.java @@ -24,7 +24,7 @@ public class Chat { }, String.class, String.class); hubConnection.onClosed((ex) -> { - if (ex.getMessage() != null) { + if (ex != null) { System.out.printf("There was an error: %s", ex.getMessage()); } }); diff --git a/src/Microsoft.AspNetCore.SignalR.Core/HubConnectionContext.cs b/src/Microsoft.AspNetCore.SignalR.Core/HubConnectionContext.cs index 09288f5a30..895a673184 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/HubConnectionContext.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/HubConnectionContext.cs @@ -21,7 +21,7 @@ namespace Microsoft.AspNetCore.SignalR { public class HubConnectionContext { - private StreamTracker _streamTracker; + private static readonly Action _cancelReader = state => ((PipeReader)state).CancelPendingRead(); private static readonly WaitCallback _abortedCallback = AbortConnection; private readonly ConnectionContext _connectionContext; @@ -32,6 +32,7 @@ namespace Microsoft.AspNetCore.SignalR private readonly long _clientTimeoutInterval; private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1); + private StreamTracker _streamTracker; private long _lastSendTimeStamp = DateTime.UtcNow.Ticks; private long _lastReceivedTimeStamp = DateTime.UtcNow.Ticks; private bool _receivedMessageThisInterval = false; @@ -356,7 +357,10 @@ namespace Microsoft.AspNetCore.SignalR { try { + var input = Input; + using (var cts = new CancellationTokenSource()) + using (var registration = cts.Token.Register(_cancelReader, input)) { if (!Debugger.IsAttached) { @@ -365,7 +369,8 @@ namespace Microsoft.AspNetCore.SignalR while (true) { - var result = await _connectionContext.Transport.Input.ReadAsync(cts.Token); + var result = await input.ReadAsync(); + var buffer = result.Buffer; var consumed = buffer.Start; var examined = buffer.End; @@ -375,6 +380,7 @@ namespace Microsoft.AspNetCore.SignalR if (result.IsCanceled) { Log.HandshakeCanceled(_logger); + await WriteHandshakeResponseAsync(new HandshakeResponseMessage("Handshake was canceled.")); return false; } @@ -447,7 +453,7 @@ namespace Microsoft.AspNetCore.SignalR } finally { - _connectionContext.Transport.Input.AdvanceTo(consumed, examined); + input.AdvanceTo(consumed, examined); } } } diff --git a/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs b/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs index 8974bd094f..f663ac7254 100644 --- a/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR.Internal; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -36,6 +37,8 @@ namespace Microsoft.Extensions.DependencyInjection public static ISignalRServerBuilder AddSignalR(this IServiceCollection services) { services.AddConnections(); + // Disable the WebSocket keep alive since SignalR has it's own + services.Configure(o => o.KeepAliveInterval = TimeSpan.Zero); services.TryAddSingleton(); services.TryAddEnumerable(ServiceDescriptor.Singleton, HubOptionsSetup>()); return services.AddSignalRCore();