DD.WellWorkover.Cloud/SignalRTestClient/Program.cs

86 lines
2.9 KiB
C#
Raw Normal View History

2023-06-02 09:13:30 +05:00
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace SignalRTestClient;
internal class Program
{
static void Main()
2023-06-02 09:13:30 +05:00
{
var connectionBuilder = new HubConnectionBuilder();
var connection = connectionBuilder
.WithUrl("http://localhost:5000/hubs/limitingParameters", connectionOptions => {
2023-06-02 09:13:30 +05:00
connectionOptions.AccessTokenProvider = AccessTokenProvider;
})
.WithAutomaticReconnect()
.AddJsonProtocol()
.ConfigureLogging(ConfigureLogging)
.Build();
connection.Reconnected += (connectionId) => Task.Run(()=> Console.WriteLine($"Reconnected {connectionId}"));
connection.Closed += (exception) => Task.Run(()=> Console.WriteLine($"Closed {exception?.Message}"));
connection.Reconnecting += (exception) => Task.Run(()=> Console.WriteLine($"Reconnecting {exception?.Message}"));
Console.WriteLine("connecting");
connection.StartAsync().Wait();
2023-07-17 11:48:52 +05:00
//Console.WriteLine("OnConnected");
connection.SendCoreAsync("OnConnectedAsync", new object[] { 1 }, CancellationToken.None).Wait();
2023-07-17 11:48:52 +05:00
var subsction = connection.On<object>("well_info_update", (str1) => {
2023-06-02 09:13:30 +05:00
Console.WriteLine(str1);
} );
//connection.On("ReceiveDataSaub");
//connection.On("ReceiveDataSpin");
Console.ReadLine();
connection.SendCoreAsync("RemoveFromGroup", new object[] { "well_1" }).Wait();
Console.WriteLine("Done!");
}
private static void ConfigureLogging(ILoggingBuilder builder)
{
ILoggerProvider provider = new LoggerProvider();
builder.AddProvider(provider);
}
private static Task<string> AccessTokenProvider()
2023-06-02 09:13:30 +05:00
{
return Task.FromResult("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImN0eSI6IkpXVCJ9.eyJpZCI6IjEiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiZGV2IiwiaWRDb21wYW55IjoiMSIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6InJvb3QiLCJuYmYiOjE2NjI1NDgxNjIsImV4cCI6MTY5NDEwNTc2MiwiaXNzIjoiYSIsImF1ZCI6ImEifQ.OEAlNzxi7Jat6pzDBTAjTbChskc-tdJthJexyWwwUKE");
}
}
internal class LoggerProvider : ILoggerProvider
{
public ILogger CreateLogger(string categoryName)
{
ILogger logger = new ConsoleLogger();
return logger;
}
public void Dispose()
{
//throw new NotImplementedException();
}
}
internal class ConsoleLogger : ILogger
{
public IDisposable? BeginScope<TState>(TState state) where TState : notnull
{
return null;
}
public bool IsEnabled(LogLevel logLevel)
{
return true;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
Console.WriteLine(formatter(state, exception));
}
}