From 4a60a8fb120cd4200aa2f33dad59d3a5783529bb Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Fri, 2 Jun 2023 09:13:30 +0500 Subject: [PATCH] Add SignalR test client --- AsbCloud.sln | 6 ++ SignalRTestClient/Program.cs | 85 ++++++++++++++++++++++ SignalRTestClient/SignalRTestClient.csproj | 14 ++++ 3 files changed, 105 insertions(+) create mode 100644 SignalRTestClient/Program.cs create mode 100644 SignalRTestClient/SignalRTestClient.csproj diff --git a/AsbCloud.sln b/AsbCloud.sln index baa10b0b..e7a39b41 100644 --- a/AsbCloud.sln +++ b/AsbCloud.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AsbCloudDb", "AsbCloudDb\As EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AsbCloudWebApi.Tests", "AsbCloudWebApi.Tests\AsbCloudWebApi.Tests.csproj", "{9CF6FBB1-9AF5-45AB-A521-24F11A79B540}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SignalRTestClient", "SignalRTestClient\SignalRTestClient.csproj", "{E6B97963-4CEA-47B6-A0C8-625FFA9B7D69}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {9CF6FBB1-9AF5-45AB-A521-24F11A79B540}.Debug|Any CPU.Build.0 = Debug|Any CPU {9CF6FBB1-9AF5-45AB-A521-24F11A79B540}.Release|Any CPU.ActiveCfg = Release|Any CPU {9CF6FBB1-9AF5-45AB-A521-24F11A79B540}.Release|Any CPU.Build.0 = Release|Any CPU + {E6B97963-4CEA-47B6-A0C8-625FFA9B7D69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E6B97963-4CEA-47B6-A0C8-625FFA9B7D69}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E6B97963-4CEA-47B6-A0C8-625FFA9B7D69}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E6B97963-4CEA-47B6-A0C8-625FFA9B7D69}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SignalRTestClient/Program.cs b/SignalRTestClient/Program.cs new file mode 100644 index 00000000..f16872bf --- /dev/null +++ b/SignalRTestClient/Program.cs @@ -0,0 +1,85 @@ +using Microsoft.AspNetCore.SignalR.Client; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace SignalRTestClient; + +internal class Program +{ + static void Main(string[] args) + { + var connectionBuilder = new HubConnectionBuilder(); + var connection = connectionBuilder + .WithUrl("http://test.digitaldrilling.ru/hubs/telemetry", connectionOptions => { + 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(); + + Console.WriteLine("AddToGroup"); + connection.SendCoreAsync("AddToGroup", new object[] { "well_1" }).Wait(); + var subsction = connection.On("UpdateProcessMap", (str1) => { + 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 AccessTokenProvider() + { + 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 state) where TState : notnull + { + return null; + } + + public bool IsEnabled(LogLevel logLevel) + { + return true; + } + + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) + { + Console.WriteLine(formatter(state, exception)); + } +} \ No newline at end of file diff --git a/SignalRTestClient/SignalRTestClient.csproj b/SignalRTestClient/SignalRTestClient.csproj new file mode 100644 index 00000000..5af7effc --- /dev/null +++ b/SignalRTestClient/SignalRTestClient.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + +