private MessageContainerInformation AuthenticationAndConnect(string ErrorInformation) { MessageContainerInformation mci = new MessageContainerInformation(); var loginfo = saveLoginInformation.Get(); if (loginfo != null) { mci = this.Connect(loginfo); } else { Authentication authentication = new Authentication(ErrorInformation); bool? result = authentication.ShowDialog(); if (result == true && !authentication.IsOpenRegistration) { mci = this.Connect(authentication.LogInfo); } else if (result == true && authentication.IsOpenRegistration) { Registration reg = new Registration(); if (reg.ShowDialog() == true) { mci = this.Connect(reg.LogInfo); } } else { Application.Current.MainWindow.Close(); mci.IsCorrectAuthentication = true; } } return(mci); }
public MainWindow() { InitializeComponent(); MessageContainerInformation mci = null; do { mci = AuthenticationAndConnect(mci != null ? mci.ErrorInformation : null); }while (mci.IsCorrectAuthentication != true); }
private MessageContainerInformation Connect(LoginInformation info) { MessageContainerInformation mci = new MessageContainerInformation(); IPEndPoint point = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8000); TcpClient client = new TcpClient(); try { client.Connect(point); stream = client.GetStream(); //Відправляємо дані авторизації string dataJSON = JsonConvert.SerializeObject(info); var data = Encoding.UTF8.GetBytes(dataJSON); stream.Write(data, 0, data.Length); //Отримуємо дані підтвердження int bytes = 0; data = new byte[256]; StringBuilder builder = new StringBuilder(); do { bytes = stream.Read(data, 0, data.Length); builder.Append(Encoding.UTF8.GetString(data, 0, bytes)); } while(stream.DataAvailable); mci = JsonConvert.DeserializeObject <MessageContainerInformation>(builder.ToString()); if (mci.IsCorrectAuthentication) { foreach (var m in mci.Messages) { CreateMessageBlock(m.User.Name, m.Text); Task.Factory.StartNew(() => GetMessage()); dckSend.IsEnabled = true; } saveLoginInformation.Save(info); Task.Factory.StartNew(() => GetMessage()); } else { //stream.Close(); client.Close(); } return(mci); } catch (Exception ex) { MessageBox.Show(ex.Message, " Error"); //stream.Close(); client.Close(); } return(mci); }
public void SendDataForOneUser(ClientObject clObj, MessageContainerInformation inf) { string sendMessage = JsonConvert.SerializeObject(inf); byte[] bmessage = UnicodeEncoding.UTF8.GetBytes(sendMessage); try { clObj.Stream.Write(bmessage, 0, bmessage.Length); } catch { Console.WriteLine("Ошибка отправки сообщения!"); } }
public void Proccess() { try { this.Stream = client.GetStream(); //Отримуємо інформацію з логіном та паролем у форматі JSON string lI = getMessage(); LoginInformation logInfo = JsonConvert.DeserializeObject <LoginInformation>(lI); MessageContainerInformation mci = new MessageContainerInformation() { IsCorrectAuthentication = false }; if (logInfo.IsLogin) { User user = db.Users.FirstOrDefault(n => n.Email == logInfo.Email); if (user != null && user.Password == logInfo.Password) { mci.IsCorrectAuthentication = true; mci.Messages = db.Messages.Include(n => n.User); Email = user.Email; } else { mci.IsCorrectAuthentication = false; mci.ErrorInformation = "Login Or Password is incorrect"; } } else { if (db.Users.Where(n => n.Email == logInfo.Email).Count() == 0) { User user = new User() { Name = logInfo.Name, Email = logInfo.Email, Password = logInfo.Password }; db.Users.Add(user); db.SaveChanges(); } else { mci.IsCorrectAuthentication = false; mci.ErrorInformation = "User Email is Already Exist"; } } server.SendDataForOneUser(this, mci); if (!mci.IsCorrectAuthentication) { client.Close(); server.RemoveConnection(this.Id); return; } // //server.SendDataForOneUser(this); while (true) { try { string messageText = String.Format($"{getMessage()}"); Message message = new Message() { Text = messageText }; User user = db.Users.FirstOrDefault(n => n.Email == this.Email); if (user != null) { message.UserId = user.Id; message.Date = DateTime.Now; db.Messages.Add(message); db.SaveChanges(); message.User = user; server.SandMessageForAll(message); } Console.WriteLine("User have written: '{0}'", message); } catch (Exception ex) { this.Stream.Close(); this.client.Close(); this.server.RemoveConnection(this.Id); Console.WriteLine(ex.Message); break; } } } catch { } }