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:
parent
acc0b7ad0d
commit
05ebd10258
|
|
@ -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();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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));
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue