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