aspnetcore/samples/ClientSample/RawSample.cs

83 lines
2.6 KiB
C#

// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Sockets;
using Microsoft.AspNetCore.Sockets.Client;
using Microsoft.Extensions.CommandLineUtils;
using Microsoft.Extensions.Logging;
namespace ClientSample
{
internal class RawSample
{
internal static void Register(CommandLineApplication app)
{
app.Command("raw", cmd =>
{
cmd.Description = "Tests a connection to an endpoint";
var baseUrlArgument = cmd.Argument("<BASEURL>", "The URL to the Chat EndPoint to test");
cmd.OnExecute(() => ExecuteAsync(baseUrlArgument.Value));
});
}
public static async Task<int> ExecuteAsync(string baseUrl)
{
baseUrl = string.IsNullOrEmpty(baseUrl) ? "http://localhost:5000/chat" : baseUrl;
var loggerFactory = new LoggerFactory();
var logger = loggerFactory.CreateLogger<Program>();
Console.WriteLine($"Connecting to {baseUrl}...");
var connection = new Connection(new Uri(baseUrl), loggerFactory);
try
{
var cts = new CancellationTokenSource();
connection.Received += (data, format) => Console.WriteLine($"{Encoding.UTF8.GetString(data)}");
connection.Closed += e => cts.Cancel();
await connection.StartAsync();
Console.WriteLine($"Connected to {baseUrl}");
Console.CancelKeyPress += (sender, a) =>
{
a.Cancel = true;
cts.Cancel();
};
while (!cts.Token.IsCancellationRequested)
{
var line = await Task.Run(() => Console.ReadLine(), cts.Token);
if (line == null)
{
break;
}
await connection.SendAsync(Encoding.UTF8.GetBytes(line), MessageType.Text, cts.Token);
}
}
catch (AggregateException aex) when (aex.InnerExceptions.All(e => e is OperationCanceledException))
{
}
catch (OperationCanceledException)
{
}
finally
{
await connection.DisposeAsync();
}
return 0;
}
}
}