new public static void Handle(string data, Connection connection) { List <Message> messages = MessageService.GetMessagesAdressedTo(connection.ConnectedUser.Name); try { foreach (Message messageToSend in messages) { Package.Write( connection.TcpClient.GetStream(), RequestConverter.ComposeMessage(messageToSend)); } //Thread.Sleep(10); //Package.Write( // connection.TcpClient.GetStream(), // RequestConverter.ComposeGetAllMessagesResponse()); } catch (Exception ex) { if (ex is NullReferenceException || ex is InvalidOperationException) { Console.WriteLine(ex); } else { throw; } } }
new public static void Handle(string data, Connection connection) { Message message = RequestConverter.DecomposeMessage(data); message.SendedTime = DateTime.UtcNow; try { MessageService.SaveMessageIfLocalIdUnique(message); message = MessageService.GetMessageById(message.Id); Package.Write( connection.TcpClient.GetStream(), RequestConverter.ComposeMessageSyncInfo(message.Id, message.SenderLocalId, message.SendedTime)); Connection receiverConnection = ConnectionController.Connections .FirstOrDefault(c => c.ConnectedUser.Name == message.Receiver); if (!(receiverConnection is null)) { Package.Write( receiverConnection.TcpClient.GetStream(), RequestConverter.ComposeMessage(message)); } } catch (Exception ex) { if (ex is NullReferenceException || ex is InvalidOperationException) { //MessageService.SaveMessage(message); // here i got exception; i must correct it; //else //throw; Console.WriteLine(ex.Message); } } }
public void SendMessage(LocalMessage message) { ServerConnectionHandler.RequestsToSend.Add(RequestConverter.ComposeMessage(message.GetMessage())); }