예제 #1
0
 // метод приема сообщений
 private void ReceiveMessages()
 {
     alive = true;
     try
     {
         while (alive)
         {
             IPEndPoint remoteIp = null;
             byte[]     data     = client.Receive(ref remoteIp);
             string     message  = Encoding.Unicode.GetString(data);
             if (message[0] == '0')
             {
                 message = message.Substring(message.IndexOf("\n") + 1);
                 string time = DateTime.Now.ToShortTimeString();
                 context.Post(delegate(object state) { ChatMessages.AppendText(time + " " + message + "\r\n"); }, null);
             }
             else if (message[0] == '1')
             {
                 //context.Post(delegate (object state) { ListOfUsers.Clear(); }, null);
                 message = message.Substring(message.IndexOf("\n"));
                 ListOfUsers.Clear();
                 context.Post(delegate(object state) { ListOfUsers.AppendText(message + "\r\n"); }, null);
             }
             else if (message[0] == '@')
             {
                 ExitChat();
             }
         }
     }
     catch (ObjectDisposedException)
     {
         if (!alive)
         {
             return;
         }
         throw;
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
예제 #2
0
 // метод приема сообщений
 private void ReceiveMessages()
 {
     alive = true;
     try
     {
         while (alive)
         {
             IPEndPoint remoteIp = null;
             byte[]     data     = client.Receive(ref remoteIp);
             string     message  = Encoding.Unicode.GetString(data);
             if (message[0] == '0')
             {
                 message = message.Substring(message.IndexOf("\n") + 1);
                 string time = DateTime.Now.ToShortTimeString();
                 context.Post(delegate(object state) { ChatMessages.AppendText(time + " " + message + "\r\n"); }, null);
                 string message_from_server = "0 \n";
                 message_from_server += ChatMessages;
                 foreach (int port in ports)
                 {
                     data = Encoding.Unicode.GetBytes(message_from_server);
                     client.Send(data, data.Length, HOST, port);
                 }
             }
             else if (message[0] == '1')
             {
                 message = message.Substring(message.IndexOf("\n"));
                 users_in_room.Add(message);
                 ListOfUsers.Clear();
                 context.Post(delegate(object state) { ListOfUsers.AppendText(message + "\r\n"); }, null);
                 string message_from_server1 = "1 \n";
                 foreach (string name in users_in_room)
                 {
                     message_from_server1 += name + "\r\n";
                 }
                 foreach (int port in ports)
                 {
                     data = Encoding.Unicode.GetBytes(message_from_server1);
                     client.Send(data, data.Length, HOST, port);
                 }
             }
             else if (message[0] == 'p')
             {
                 message = message.Substring(message.IndexOf("\n") + 1);
                 ports.Add(int.Parse(message));
             }
             else if (message[0] == 'e')
             {
                 message = message.Substring(message.IndexOf("\n") + 1);
                 ports.Remove(int.Parse(message));
             }
             else if (message[0] == 'o')
             {
                 message = message.Substring(message.IndexOf("\n") + 1);
                 users_in_room.Remove(message);
                 ListOfUsers.Clear();
                 context.Post(delegate(object state) { ListOfUsers.AppendText(message + "\r\n"); }, null);
                 string message_from_server1 = "1 \n";
                 foreach (string name in users_in_room)
                 {
                     message_from_server1 += name + "\r\n";
                 }
                 foreach (int port in ports)
                 {
                     data = Encoding.Unicode.GetBytes(message_from_server1);
                     client.Send(data, data.Length, HOST, port);
                 }
             }
         }
     }
     catch (ObjectDisposedException)
     {
         if (!alive)
         {
             return;
         }
         throw;
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }