public void AddMessageButton_Click(object sender, EventArgs e) { BatProtocol bob = new BatProtocol { Type = "PM", Message = WriteBox.Text, UserName = whichUser }; SendProtocol(bob); WriteBox.Text = ""; }
public void SendProtocol(Object input) { BatProtocol p = (BatProtocol)input; //client = new TcpClient(p.RecieverIP, p.RecieverPort); NetworkStream n = client.GetStream(); BinaryWriter w = new BinaryWriter(n); string protocol = JsonConvert.SerializeObject(input); w.Write(protocol); }
public void SendProtocol(Object input) { BatProtocol p = (BatProtocol)input; NetworkStream n = client.GetStream(); BinaryWriter w = new BinaryWriter(n); string protocol = JsonConvert.SerializeObject(p); w.Write(protocol); }
public void Listen() { string message = "In case of no inputstring: This is the response"; try { while (true) { //Här kommer svarsobjektet NetworkStream n = client.GetStream(); message = new BinaryReader(n).ReadString(); //dekoda message till objekt BatProtocol deSerializedMessage = JsonConvert.DeserializeObject <BatProtocol>(message); //Console.WriteLine("MessageType: " + deSerializedMessage.Type); this.messType = deSerializedMessage.Type; if (deSerializedMessage.Type == "Ok") { List <String> listPeople; if (deSerializedMessage.Userlist == null) { listPeople = new List <string>(); } else { listPeople = deSerializedMessage.Userlist; } var x = new Chatbox(Context, client, userName, listPeople); x.ShowDialog(); } else if (deSerializedMessage.Type == "SM") { //messType = deSerializedMessage.Message; } } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public void Listen() { string message = "In case of no inputstring: This is the response"; try { while (true) { //Här kommer svarsobjektet NetworkStream n = client.GetStream(); message = new BinaryReader(n).ReadString(); //dekoda message till objekt BatProtocol deSerializedMessage = JsonConvert.DeserializeObject <BatProtocol>(message); //Console.WriteLine("MessageType: " + deSerializedMessage.Type); this.messType = deSerializedMessage.Type; if (deSerializedMessage.Type == "Ok") { if (deSerializedMessage.Userlist != null) { Listbox_of_Users.DataSource = deSerializedMessage.Userlist; } } else if (deSerializedMessage.Type == "SM") { timeStamp = DateTime.Now.ToShortTimeString(); ShowChatBox.Items.Add($"({timeStamp}) {deSerializedMessage.UserName}:{ deSerializedMessage.Message.ToString()}"); if (deSerializedMessage.Userlist != null) { Listbox_of_Users.DataSource = deSerializedMessage.Userlist; } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private void Button_connect_Click(object sender, EventArgs e) { if (TextBox_receiverIP.Text == "" || textBox_receiverPort.Text == "") { MessageBox.Show("Please enter the proper IP/Port information into the receiver fields"); } if (textBox_userName.Text == "" || textBox_userPassword.Text == "") { MessageBox.Show("Please enter proper login information"); } else { userName = textBox_userName.Text; user_PassWord = textBox_userPassword.Text; reciever_IP = TextBox_receiverIP.Text; reciever_port = Convert.ToInt32(textBox_receiverPort.Text); BatProtocol p = new BatProtocol() { Type = "Login", Version = 1, UserName = userName, Password = user_PassWord, RecieverIP = reciever_IP, RecieverPort = reciever_port, }; client = new TcpClient(p.RecieverIP, p.RecieverPort); Thread batListener = new Thread(Listen); batListener.Start(); Thread batThread = new Thread(SendProtocol); batThread.Start(p); batThread.Join(); } }
private void ReadMessage(string message) { bool doubleLogin = false; BatProtocol deSerializedMessage = JsonConvert.DeserializeObject <BatProtocol>(message); //Testar om message är av typen login if (deSerializedMessage.Type == "Login") { Console.WriteLine("logging in..."); //Kolla om redan inloggad if (NameList != null && NameList.Contains(deSerializedMessage.UserName)) { Console.WriteLine(deSerializedMessage.UserName + "Redan inloggad"); //Skickar felmessage i retur doubleLogin = true; NetworkStream n = tcpclient.GetStream(); BatProtocol ok = new BatProtocol { Type = "DoubleLogin" }; new BinaryWriter(n).Write(JsonConvert.SerializeObject(ok)); } //kollar om username finns i databasen else if (Context.BatUsers.ToList() .Exists(u => u.Name == deSerializedMessage.UserName)) { if (NameList == null) { NameList = new List <string> { deSerializedMessage.UserName }; } else { NameList.Add(deSerializedMessage.UserName); } Console.WriteLine(deSerializedMessage.UserName + " User Exists"); } else if (!(Context.BatUsers.ToList().Exists(s => s.Name.Equals(deSerializedMessage.UserName)))) { Console.WriteLine(deSerializedMessage.UserName + " does not exists. Creating a new user..."); try { BatUsers newUser = new BatUsers { Name = deSerializedMessage.UserName, Password = deSerializedMessage.Password }; Context.BatUsers.Add(newUser); Context.SaveChanges(); Console.WriteLine("Creation succesful!"); if (NameList == null) { NameList = new List <string> { deSerializedMessage.UserName }; } else { NameList.Add(deSerializedMessage.UserName); } } catch (Exception ex) { throw ex; } } //Kollar om lösenord matchar med det som är lagt in i databasen under samma login. if (!doubleLogin && Context.BatUsers.ToList() .Find(u => u.Name == deSerializedMessage.UserName) .Password == deSerializedMessage.Password) { Console.WriteLine("Login Successful"); //Skickar message i retur NetworkStream n = tcpclient.GetStream(); BatProtocol ok = new BatProtocol { Type = "Ok" }; if (NameList != null) { ok.Userlist = NameList; } new BinaryWriter(n).Write(JsonConvert.SerializeObject(ok)); } } else if (deSerializedMessage.Type == "PM") { Console.WriteLine(deSerializedMessage.UserName + " PM: " + deSerializedMessage.Message); NetworkStream n = tcpclient.GetStream(); deSerializedMessage.Type = "SM"; if (NameList != null) { deSerializedMessage.Userlist = NameList; } //new BinaryWriter(n).Write(JsonConvert.SerializeObject(deSerializedMessage)); myServer.Broadcast(this, JsonConvert.SerializeObject(deSerializedMessage)); } else { Console.WriteLine("Unhandled message Type"); } }