ExampleSignalR/HubListener/Listeners/ChatListener.cs

35 lines
845 B
C#
Raw Normal View History

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}");
}
}
}