Make baseUrl work again (#1889)

-The client sample can now switch between TCP and HTTP connection layer by URI scheme
- Support net.tcp URI scheme for the TCP connection
This commit is contained in:
David Fowler 2018-04-07 12:07:25 -07:00 committed by GitHub
parent acc0b7ad0d
commit 05ebd10258
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 8 deletions

View File

@ -28,16 +28,25 @@ namespace ClientSample
public static async Task<int> ExecuteAsync(string baseUrl) public static async Task<int> ExecuteAsync(string baseUrl)
{ {
var endpoint = new IPEndPoint(IPAddress.Loopback, 9001); var uri = baseUrl == null ? new Uri("net.tcp://127.0.0.1:9001") : new Uri(baseUrl);
Console.WriteLine("Connecting to {0}", endpoint); Console.WriteLine("Connecting to {0}", uri);
var connection = new HubConnectionBuilder() var connectionBuilder = new HubConnectionBuilder()
.WithEndPoint(endpoint)
.WithLogging(logging => .WithLogging(logging =>
{ {
logging.AddConsole(); logging.AddConsole();
logging.SetMinimumLevel(LogLevel.Trace); logging.SetMinimumLevel(LogLevel.Trace);
}) });
.Build();
if (uri.Scheme == "net.tcp")
{
connectionBuilder.WithEndPoint(uri);
}
else
{
connectionBuilder.WithUrl(uri);
}
var connection = connectionBuilder.Build();
try try
{ {
@ -48,7 +57,7 @@ namespace ClientSample
await ConnectAsync(connection); await ConnectAsync(connection);
Console.WriteLine("Connected to {0}", endpoint); Console.WriteLine("Connected to {0}", uri);
var sendCts = new CancellationTokenSource(); var sendCts = new CancellationTokenSource();

View File

@ -9,7 +9,27 @@ namespace Microsoft.AspNetCore.SignalR.Client
{ {
public static class TcpHubConnectionBuilderExtensions public static class TcpHubConnectionBuilderExtensions
{ {
public static IHubConnectionBuilder WithEndPoint(this IHubConnectionBuilder builder, IPEndPoint endPoint) public static IHubConnectionBuilder WithEndPoint(this IHubConnectionBuilder builder, Uri uri)
{
if (!string.Equals(uri.Scheme, "net.tcp", StringComparison.Ordinal))
{
throw new InvalidOperationException($"URI Scheme {uri.Scheme} not supported.");
}
IPEndPoint endPoint;
if (string.Equals(uri.Host, "localhost"))
{
endPoint = new IPEndPoint(IPAddress.Loopback, uri.Port);
}
else
{
endPoint = new IPEndPoint(IPAddress.Parse(uri.Host), uri.Port);
}
return builder.WithEndPoint(endPoint);
}
public static IHubConnectionBuilder WithEndPoint(this IHubConnectionBuilder builder, EndPoint endPoint)
{ {
builder.WithConnectionFactory(() => new TcpConnection(endPoint)); builder.WithConnectionFactory(() => new TcpConnection(endPoint));