Expose ConnectionId on .NET Client (#8668)

This commit is contained in:
Mikael Mengistu 2019-03-27 17:30:43 -07:00 committed by GitHub
parent 63a4b5c27a
commit 017f409fe4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 62 additions and 0 deletions

View File

@ -26,6 +26,7 @@ namespace Microsoft.AspNetCore.SignalR.Client
public static readonly System.TimeSpan DefaultServerTimeout;
public HubConnection(Microsoft.AspNetCore.SignalR.Client.IConnectionFactory connectionFactory, Microsoft.AspNetCore.SignalR.Protocol.IHubProtocol protocol, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory) { }
public HubConnection(Microsoft.AspNetCore.SignalR.Client.IConnectionFactory connectionFactory, Microsoft.AspNetCore.SignalR.Protocol.IHubProtocol protocol, System.IServiceProvider serviceProvider, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory) { }
public string ConnectionId { get { throw null; } }
public System.TimeSpan HandshakeTimeout { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
public System.TimeSpan KeepAliveInterval { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
public System.TimeSpan ServerTimeout { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }

View File

@ -54,6 +54,7 @@ namespace Microsoft.AspNetCore.SignalR.Client
private long _nextActivationSendPing;
private bool _disposed;
private bool _hasInherentKeepAlive;
private string _connectionId;
private CancellationToken _uploadStreamToken;
@ -123,6 +124,13 @@ namespace Microsoft.AspNetCore.SignalR.Client
/// </summary>
public TimeSpan HandshakeTimeout { get; set; } = DefaultHandshakeTimeout;
/// <summary>
/// Gets the connection's current Id. This value will be cleared when the connection is stopped and will have a new value every time the connection is (re)established.
/// This value will be null if the negotiation step is skipped via HttpConnectionOptions or if the WebSockets transport is explicitly specified because the
/// client skips negotiation in that case as well.
/// </summary>
public string ConnectionId => _connectionId;
/// <summary>
/// Indicates the state of the <see cref="HubConnection"/> to the server.
/// </summary>
@ -345,6 +353,7 @@ namespace Microsoft.AspNetCore.SignalR.Client
// Start the connection
var connection = await _connectionFactory.ConnectAsync(_protocol.TransferFormat);
_connectionId = connection.ConnectionId;
var startingConnectionState = new ConnectionState(connection, this);
_hasInherentKeepAlive = connection.Features.Get<IConnectionInherentKeepAliveFeature>()?.HasInherentKeepAlive ?? false;
@ -412,6 +421,8 @@ namespace Microsoft.AspNetCore.SignalR.Client
(_serviceProvider as IDisposable)?.Dispose();
_disposed = true;
}
_connectionId = null;
}
finally
{

View File

@ -155,6 +155,41 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests
}
}
[Theory]
[MemberData(nameof(HubProtocolsAndTransportsAndHubPaths))]
[LogLevel(LogLevel.Trace)]
public async Task CanAccessConnectionIdFromHubConnection(string protocolName, HttpTransportType transportType, string path)
{
var protocol = HubProtocols[protocolName];
using (StartServer<Startup>(out var server))
{
var connection = CreateHubConnection(server.Url, path, transportType, protocol, LoggerFactory);
try
{
Assert.Null(connection.ConnectionId);
await connection.StartAsync().OrTimeout();
var originalClientConnectionId = connection.ConnectionId;
var connectionIdFromServer = await connection.InvokeAsync<string>(nameof(TestHub.GetCallerConnectionId)).OrTimeout();
Assert.Equal(connection.ConnectionId, connectionIdFromServer);
await connection.StopAsync().OrTimeout();
Assert.Null(connection.ConnectionId);
await connection.StartAsync().OrTimeout();
connectionIdFromServer = await connection.InvokeAsync<string>(nameof(TestHub.GetCallerConnectionId)).OrTimeout();
Assert.NotEqual(originalClientConnectionId, connectionIdFromServer);
Assert.Equal(connection.ConnectionId, connectionIdFromServer);
}
catch (Exception ex)
{
LoggerFactory.CreateLogger<HubConnectionTests>().LogError(ex, "{ExceptionType} from test", ex.GetType().FullName);
throw;
}
finally
{
await connection.DisposeAsync().OrTimeout();
}
}
}
[Theory]
[MemberData(nameof(HubProtocolsAndTransportsAndHubPaths))]
[LogLevel(LogLevel.Trace)]

View File

@ -36,6 +36,11 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests
await Clients.Client(Context.ConnectionId).SendAsync("NoClientHandler");
}
public string GetCallerConnectionId()
{
return Context.ConnectionId;
}
public ChannelReader<string> StreamEcho(ChannelReader<string> source) => TestHubMethodsImpl.StreamEcho(source);
public string GetUserIdentifier()
@ -110,6 +115,11 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests
await Clients.Client(Context.ConnectionId).NoClientHandler();
}
public string GetCallerConnectionId()
{
return Context.ConnectionId;
}
public ChannelReader<string> StreamEcho(ChannelReader<string> source) => TestHubMethodsImpl.StreamEcho(source);
}
@ -135,6 +145,11 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests
await Clients.Client(Context.ConnectionId).NoClientHandler();
}
public string GetCallerConnectionId()
{
return Context.ConnectionId;
}
public ChannelReader<string> StreamEcho(ChannelReader<string> source) => TestHubMethodsImpl.StreamEcho(source);
}