public bool Execute() { Console.Write("Give name for new discussion: "); String name = Console.ReadLine(); Console.Write("Give name of receiver: "); String userTo = Console.ReadLine(); Console.Write("Write first message: "); String msg = Console.ReadLine(); // serialize new chat ChatRoom c = new ChatRoom(nickName, userTo, name); c.addMessage(nickName, msg); String json = JsonConvert.SerializeObject(c, Formatting.None); // send query to server ChatCommand cmd = new ChatCommand("add", nickName, json); StreamWriter writer = new StreamWriter(tcpClient.GetStream()); writer.WriteLine(cmd.ToJson()); writer.Flush(); return false; }
public DemoChat() { bool exit = false; String nickName; // system running on localhost tcpClient = new System.Net.Sockets.TcpClient(); try { tcpClient.Connect("localhost", 4296); } catch (Exception ex) { Console.WriteLine("Failed to connect: "); } Thread chatThread = new Thread(new ThreadStart(run)); chatThread.Start(); Console.Clear(); Console.WriteLine("Welcome to simple chat!"); // get user nick name for discussions nickName = getNicName(); // send query to server ChatCommand cmd = new ChatCommand("list", nickName); StreamWriter writer = new StreamWriter(tcpClient.GetStream()); //Console.WriteLine(cmd.ToJson()); writer.WriteLine(cmd.ToJson()); writer.Flush(); // program main loop while (exit == false) { Console.Clear(); Console.WriteLine("Discussions for (" + nickName + ")"); // discussionsAA if (chatRooms != null) { int i = 0; foreach (ChatRoom r in chatRooms) { Console.WriteLine(i + ") " + r.UserA + " ( " + r.StartTime + ": " + r.Name + " )"); i++; } } Console.WriteLine(); Console.Write("Enter command (help to display help): "); String line = Console.ReadLine(); var command = Parser.Parse(line, nickName, tcpClient); // chat room has different parameters if (command.GetType() == typeof(OpenCommand)) { // open chat room ChatRoom r = chatRooms.ElementAt(((OpenCommand)command).Index); ((OpenCommand)command).Room = r; exit = command.Execute(); } else { if (command.GetType() == typeof(SortCommand)) { exit = command.Execute(); switch (((SortCommand)command).Sort) { case 1: chatRooms = chatRooms.OrderByDescending(o => o.StartTime).ToList(); break; case 2: chatRooms = chatRooms.OrderBy(o => o.Name).ToList(); break; case 3: chatRooms = chatRooms.OrderBy(o => o.Messages.ElementAt(o.Messages.Count - 1).Time).ToList(); break; } } else { exit = command.Execute(); } } } tcpClient.Close(); // terminate program ExitProcess(0); }
public bool Execute() { Console.Clear(); if (r != null) { foreach (ChatMessage m in r.Messages) { Console.WriteLine(m.Time + " " + m.UserFrom + ": " + m.Message); } Console.WriteLine(); Console.Write("Enter new message: "); String msg = Console.ReadLine(); if (msg.Length > 2) { // simply clone chat room id ChatRoom c = new ChatRoom(); c.Id = r.Id; c.addMessage(nickName, msg); String json = JsonConvert.SerializeObject(c, Formatting.None); // send query to server ChatCommand cmd = new ChatCommand("msg", nickName, json); StreamWriter writer = new StreamWriter(tcpClient.GetStream()); writer.WriteLine(cmd.ToJson()); writer.Flush(); } else { Console.WriteLine("Too short message!"); } } return false; }
public bool Execute() { // send query to server ChatCommand cmd = new ChatCommand("list", nickName); StreamWriter writer = new StreamWriter(tcpClient.GetStream()); writer.WriteLine(cmd.ToJson()); writer.Flush(); return false; }