ExampleSignalR/HubListener/ChatService.cs

33 lines
983 B
C#
Raw Normal View History

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), $"проверка связи");
}
}
}