private Task StartRecive() { return(Task.Run(async() => { while (!_cancellationToken.IsCancellationRequested) { using (_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { try { _socket.Connect(_server, _port); await Login(); while (_commandQueue.Any()) { var queued = _commandQueue.Dequeue(); await SendArray(queued); } byte[] array = new byte[256]; while (_socket.Connected && !_cancellationToken.IsCancellationRequested) { int length = _socket.Receive(array); if (length > 0) { _timeout = 0; OnRecive.Invoke(this, array[0..length]); }
public void SendMessage(User sender, Message message) { if (this == sender) { Console.WriteLine("Нельзя отправить сообщение отправителю."); return; } message.Sender = sender; message.Reciver = this; message.SendTime = DateTime.Now; OnSend?.Invoke(this, message); OnRecive?.Invoke(this, message); }