public void Process() { try { Stream = client.GetStream(); // получаем имя пользователя string message = GetMessage(); userName = message; message = userName + " вошел в чат"; // посылаем сообщение о входе в чат всем подключенным пользователям server.BroadcastMessage(message, this.Id); Console.WriteLine(message); // в бесконечном цикле получаем сообщения от клиента while (true) { try { message = GetMessage(); message = String.Format("{0}: {1}", userName, message); ListView listView1 = new ListView(); listView1.Items.Add(String.Format("{0}: {1}", userName, message)); Console.WriteLine(message); server.BroadcastMessage(message, this.Id); } catch { message = String.Format("{0}: покинул чат", userName); ListView listView1 = new ListView(); listView1.Items.Add(String.Format("{0}: покинул чат", userName)); Console.WriteLine(message); server.BroadcastMessage(message, this.Id); break; } } } catch (Exception e) { Console.WriteLine(e.Message); } finally { // в случае выхода из цикла закрываем ресурсы server.RemoveConnection(this.Id); Close(); } }
public void Process() { try { Stream = client.GetStream(); string message; // получаем имя пользователя // посылаем сообщение о входе в чат всем подключенным пользователям // в бесконечном цикле получаем сообщения от клиента while (true) { try { message = GetMessage(); var parts = message.Split(' '); string a = Convert.ToString(parts[0]); if (a == "/send") { Console.WriteLine(a); string from = Convert.ToString(parts[2]); string to = Convert.ToString(parts[4]); Console.WriteLine(to); Console.WriteLine(from); string text = message.Remove(0, message.IndexOf(':') + 1); string m = from + ":" + text + Environment.NewLine; MySqlCommand command = new MySqlCommand();; string connectionString, commandString; connectionString = "Data source=79.174.44.240;UserId=triu;Password=120evazus;database=triumph;"; MySqlConnection connection = new MySqlConnection(connectionString); commandString = string.Format("SELECT Ip FROM chat WHERE Nickname='{0}'", to); command.CommandText = commandString; command.Connection = connection; MySqlDataReader reader; command.Connection.Open(); reader = command.ExecuteReader(); while (reader.Read()) { to = reader["Ip"].ToString(); } reader.Close(); command.Connection.Close(); server.BroadcastMessage(m, from, to); } } catch (Exception e) { string connectionString, sss; connectionString = "Data source=79.174.44.240;UserId=triu;Password=120evazus;database=triumph;"; MySqlConnection connection2 = new MySqlConnection(connectionString); if (ip.ToString() != "192.168.1.1") { sss = String.Format("UPDATE chat SET Online='false' WHERE Ip='{0}'", ip); } else { sss = String.Format("UPDATE chat SET Online='false' WHERE Ip='79.174.44.240'", ip); } MySqlCommand cmd = new MySqlCommand(sss, connection2); cmd.Connection.Open(); cmd.ExecuteNonQuery(); cmd.Connection.Close(); Console.WriteLine(e); message = String.Format("{0}: покинул чат", this.Id); Console.WriteLine(message); break; } } } catch (Exception e) { Console.WriteLine(e.Message); } finally { // в случае выхода из цикла закрываем ресурсы server.RemoveConnection(this.Id); Close(); } }
public void UserInit() { try { Stream = Client.GetStream(); string Message = GetMessage(); UserName = Message; DateTime time = DateTime.Now; Message = $"[{time.ToString("HH:mm")}] Server: {Message} joined chat\n"; Server.BroadcastMessage(Message, this.Id); Console.WriteLine(Message); while (true) { try { Message = GetMessage(); if (Message.Equals("UserDisconnect")) { Server.DeleteConnetion(this.Id); DateTime time2 = DateTime.Now; Message = $"[{time2.ToString("HH:mm")}] Server: {UserName} left chat...\n"; Console.WriteLine(Message); Server.BroadcastMessage(Message, this.Id); Close(); break; } else if (Message.StartsWith("pv")) { string[] parts = Message.Split(' '); var string1 = parts[0]; var string2 = parts[1]; var theRest = Message .Replace(string1, "") .Replace(string2, ""); Server.SendPrivateMessage(theRest, string2, this.Id, this.UserName); } else if (Message.StartsWith("joinRoom")) { string[] parts = Message.Split(' '); var string1 = parts[0]; var string2 = parts[1]; JoinRoom(string2); Server.JoinRoomInfo(Id); } else if (Message.StartsWith("leaveRoom")) { string[] parts = Message.Split(' '); var string1 = parts[0]; var string2 = parts[1]; DateTime time2 = DateTime.Now; if (LeaveRoom(string2)) { Server.LeaveRoomInfo(Id, $"[{time2.ToString("HH:mm")}] Server: opuściłeś room...\n"); } else { Server.LeaveRoomInfo(Id, $"[{time2.ToString("HH:mm")}] Server: nie jesteś w tym roomie...\n"); } } else if (Message.StartsWith("room")) { string[] parts = Message.Split(' '); var string1 = parts[0]; var string2 = parts[1]; var theRest = Message .Replace(string1, "") .Replace(string2, ""); Server.RoomMessage(theRest, string2, this.Id); } else { Console.WriteLine(Message); Server.BroadcastMessage(Message, this.Id); } } catch { //Message = String.Format("{0}: leave...", UserName); ////Console.WriteLine(Message); //Server.BroadcastMessage(Message, this.Id); //break; } } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Server.DeleteConnetion(this.Id); Close(); } }