From dc5a47d749ca288f705f886abaea13299b1ddda5 Mon Sep 17 00:00:00 2001 From: Roman Efremov Date: Tue, 12 Nov 2024 13:54:41 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BB=D0=B8=D0=B5=D0=BD=D1=82=20=D0=B2?= =?UTF-8?q?=20=D0=B2=D0=B8=D0=B4=D0=B5=20=D0=BA=D0=BE=D0=BD=D1=81=D0=BE?= =?UTF-8?q?=D0=BB=D1=8C=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=BF=D1=80=D0=B8=D0=BB?= =?UTF-8?q?=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HubListener/ChatService.cs | 32 +++++++++++++++++++++ HubListener/HubListener.csproj | 17 +++++++++++ HubListener/Interfaces/IChatListener.cs | 11 +++++++ HubListener/Listeners/Base/HubListener.cs | 35 +++++++++++++++++++++++ HubListener/Listeners/ChatListener.cs | 34 ++++++++++++++++++++++ HubListener/Program.cs | 13 +++++++++ 6 files changed, 142 insertions(+) create mode 100644 HubListener/ChatService.cs create mode 100644 HubListener/HubListener.csproj create mode 100644 HubListener/Interfaces/IChatListener.cs create mode 100644 HubListener/Listeners/Base/HubListener.cs create mode 100644 HubListener/Listeners/ChatListener.cs create mode 100644 HubListener/Program.cs diff --git a/HubListener/ChatService.cs b/HubListener/ChatService.cs new file mode 100644 index 0000000..b14ca83 --- /dev/null +++ b/HubListener/ChatService.cs @@ -0,0 +1,32 @@ +using HubListener.Interfaces; +using Microsoft.Extensions.Hosting; + +namespace HubListener +{ + internal class ChatService : IHostedService + { + private readonly IHostApplicationLifetime _hostApplicationLifetime; + private readonly IChatListener _chatListener; + public ChatService(IHostApplicationLifetime hostApplicationLifetime, IChatListener chatListener) { + _hostApplicationLifetime = hostApplicationLifetime; + _chatListener = chatListener; + } + + public Task StartAsync(CancellationToken cancellationToken) + { + _hostApplicationLifetime.ApplicationStarted.Register(StartChat); + + return Task.CompletedTask; + } + + public Task StopAsync(CancellationToken cancellationToken) + => Task.CompletedTask; + + private async void StartChat() + { + await _chatListener.Connect(); + await Task.Delay(5000); // Даём другому клиенту время для запуска + await _chatListener.SendMessage(nameof(ChatService), $"проверка связи"); + } + } +} diff --git a/HubListener/HubListener.csproj b/HubListener/HubListener.csproj new file mode 100644 index 0000000..73ced01 --- /dev/null +++ b/HubListener/HubListener.csproj @@ -0,0 +1,17 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + + + + diff --git a/HubListener/Interfaces/IChatListener.cs b/HubListener/Interfaces/IChatListener.cs new file mode 100644 index 0000000..abc4b57 --- /dev/null +++ b/HubListener/Interfaces/IChatListener.cs @@ -0,0 +1,11 @@ +using Microsoft.AspNetCore.SignalR.Client; + +namespace HubListener.Interfaces +{ + internal interface IChatListener + { + Task Connect(); + Task Disconnect(); + Task SendMessage(string user, string message); + } +} \ No newline at end of file diff --git a/HubListener/Listeners/Base/HubListener.cs b/HubListener/Listeners/Base/HubListener.cs new file mode 100644 index 0000000..e1e1699 --- /dev/null +++ b/HubListener/Listeners/Base/HubListener.cs @@ -0,0 +1,35 @@ +using Microsoft.AspNetCore.SignalR.Client; + +namespace HubListener.Listeners.Base +{ + internal class HubListener + { + public HubConnection hubConnection; + internal HubListener() + { + hubConnection = new HubConnectionBuilder() + .WithUrl("https://localhost:7083/chatHub") // ...appsettings.json + .Build(); + } + + public async Task Connect() + { + try + { + await hubConnection.StartAsync(); + Console.WriteLine("Чат подключен..."); + } + catch (Exception ex) + { + Console.WriteLine($"Ошибка подключения: {ex.Message}"); + } + } + + public async Task Disconnect() + { + + await hubConnection.StopAsync(); + Console.WriteLine("Чат отключен..."); + } + } +} diff --git a/HubListener/Listeners/ChatListener.cs b/HubListener/Listeners/ChatListener.cs new file mode 100644 index 0000000..25a1e05 --- /dev/null +++ b/HubListener/Listeners/ChatListener.cs @@ -0,0 +1,34 @@ +using HubListener.Interfaces; +using Microsoft.AspNet.SignalR.Messaging; +using Microsoft.AspNetCore.SignalR.Client; + +namespace HubListener.Listeners +{ + internal class ChatListener : Base.HubListener, IChatListener + { + public ChatListener() { + hubConnection.On("ReceiveMessage", (user, message) => + { + ShowLocalMessage(user, message); + }); + } + + public async Task SendMessage(string user, string message) + { + try + { + await hubConnection.InvokeAsync("SendMessage", user, message); + } + catch (Exception ex) + { + Console.WriteLine($"Ошибка отправки: {ex.Message}"); + } + } + + private static void ShowLocalMessage(string user, string message) + { + var userMessagePart = user == null ? string.Empty : $"{user} сказал"; + Console.WriteLine($"{userMessagePart} {message}"); + } + } +} diff --git a/HubListener/Program.cs b/HubListener/Program.cs new file mode 100644 index 0000000..7b291de --- /dev/null +++ b/HubListener/Program.cs @@ -0,0 +1,13 @@ +using HubListener.Interfaces; +using HubListener.Listeners; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +await Host.CreateDefaultBuilder(args) + .ConfigureServices(services => + { + services.AddScoped(); + services.AddHostedService(); + }) + .Build() + .RunAsync(); \ No newline at end of file