35 lines
845 B
C#
35 lines
845 B
C#
|
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}");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|