websockets-client/AvaloniaChatV2/ViewModels/MainWindowViewModel.cs
2025-03-14 15:34:03 +03:00

42 lines
1.0 KiB
C#

using System;
using System.Collections.ObjectModel;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using AvaloniaChatV2.Data;
using AvaloniaChatV2.Views;
using ReactiveUI;
namespace AvaloniaChatV2.ViewModels;
public class MainWindowViewModel : ViewModelBase
{
private readonly ClientWebSocketHandler _clientWebSocketHandler = new ();
private string _currentMessage = String.Empty;
public string CurrentMessage
{
get => _currentMessage;
set => this.RaiseAndSetIfChanged(ref _currentMessage, value);
}
public ObservableCollection<string> Messages { get; set; } = new();
public MainWindowViewModel()
{
_clientWebSocketHandler.OnReceiveMessage += DisplayMessage;
}
public async Task SendMessage(string message)
{
Messages.Add(message);
await _clientWebSocketHandler.SendMessage(message);
CurrentMessage = String.Empty;
}
private void DisplayMessage(string message)
{
Messages.Add(message);
}
}