コード例 #1
0
 private void receiveFromServerThread()
 {
     while (true)
     {
         string data = tcpModel.receiveData();
         string[] value = data.Split(' ');
         switch(value[0])
         {
             case "room":
                 int rommId = int.Parse(value[1]);
                 int userOffsetInRoom = int.Parse(value[2]);
                 using (GameForm gameForm = new GameForm(tcpModel, rommId, userOffsetInRoom)) 
                 {
                     this.Hide();
                     gameForm.ShowDialog();
                     //gameForm.Close();
                     //this.Show();
                     this.Close();
                 }
                 break;
             case "user":
                 tcpModel.setID(int.Parse(value[1]));
                 tcpModel.sendData("join ");
                 break;
             default:
                 break;
         }
     }
 }
コード例 #2
0
        public void socketReceive()
        {
            int recvLength = 0;

            try
            {
                do
                {
                    recvLength = socketClient.Receive(data);
                    if (recvLength > 0)
                    {
                        string command = Encoding.Default.GetString(data).Trim();
                        Array.Clear(data, 0, data.Length);
                        //MessageBox.Show(command);
                        string[] commands = command.Split('`');
                        for (int i = 1; i < commands.Length; i++)
                        {
                            string[] line = commands[i].Split(',');
                            if (line[0] == "joinSuccess")
                            {
                                my_id = int.Parse(line[1]);
                            }
                            else if (line[0] == "color" && line[1] == "true")
                            {
                                GameForm gameForm = new GameForm();
                                gameForm.Ready(my_id, nowColor, socketClient);
                                this.Hide();
                                gameForm.ShowDialog();
                                this.Close();
                            }
                            else if (line[0] == "color" && line[1] == "false")
                            {
                                MessageBox.Show("Другой участник выбрал этот цвет, попробуйте другой!");
                            }
                        }
                    }
                } while (true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }