Клиент в виде консольного приложения
This commit is contained in:
parent
420edfc4f4
commit
dc5a47d749
32
HubListener/ChatService.cs
Normal file
32
HubListener/ChatService.cs
Normal file
@ -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), $"проверка связи");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
17
HubListener/HubListener.csproj
Normal file
17
HubListener/HubListener.csproj
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNet.SignalR.SelfHost" Version="2.4.3" />
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.10" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.1" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
11
HubListener/Interfaces/IChatListener.cs
Normal file
11
HubListener/Interfaces/IChatListener.cs
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
35
HubListener/Listeners/Base/HubListener.cs
Normal file
35
HubListener/Listeners/Base/HubListener.cs
Normal file
@ -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("Чат отключен...");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
34
HubListener/Listeners/ChatListener.cs
Normal file
34
HubListener/Listeners/ChatListener.cs
Normal file
@ -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<string, string>("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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
13
HubListener/Program.cs
Normal file
13
HubListener/Program.cs
Normal file
@ -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<IChatListener, ChatListener>();
|
||||||
|
services.AddHostedService<HubListener.ChatService>();
|
||||||
|
})
|
||||||
|
.Build()
|
||||||
|
.RunAsync();
|
Loading…
Reference in New Issue
Block a user