Beispiel #1
0
 private void lst_OnlineUser_DoubleClick(object sender, EventArgs e)
 {
     if (lst_OnlineUser.Text != textBox1.Text && lst_OnlineUser.SelectedIndex > 0)
     {
         if (CR == null)
         {
             CR           = new ChattingRom();
             CR.Text      = lst_OnlineUser.SelectedItem.ToString();
             CR.talker    = "" + lst_OnlineUser.SelectedItem + ",";
             CR.user      = textBox1.Text;
             CR.chatting += new Chatting(chatting);
             CR.Show();
         }
         else
         {
             CR.Text   = lst_OnlineUser.SelectedItem.ToString();
             CR.talker = "" + lst_OnlineUser.SelectedItem + ",";
             CR.user   = textBox1.Text;
             //       CR.chatting += new Chatting(chatting);
         }
     }
 }
Beispiel #2
0
        /// <summary>
        /// 處理接收的伺服器收據
        /// </summary>
        private void ReceiveData()
        {
            string receiveString = null;

            while (!isExit)
            {
                ReceiveMessageDelegate d      = new ReceiveMessageDelegate(receiveMessage);
                IAsyncResult           result = d.BeginInvoke(out receiveString, null, null);
                //使用輪詢方式來盤點非同步作業是否完成
                while (!result.IsCompleted)
                {
                    if (isExit)
                    {
                        break;
                    }
                    Thread.Sleep(250);
                }
                //獲取Begin方法的返回值所有輸入/輸出參數
                d.EndInvoke(out receiveString, result);
                if (receiveString == null)
                {
                    if (!isExit)
                    {
                        MessageBox.Show("與伺服器失去聯繫");
                    }
                    break;
                }
                string[] splitString = receiveString.Split(',');
                string   command     = splitString[0].ToLower();

                switch (command)
                {
                case "login":       //格式: login,用戶名
                    AddOnline(splitString[1]);
                    break;

                case "logout":      //格式: logout,用戶名
                    RemoveUserName(splitString[1]);
                    break;

                case "talk":        //格式: talk,用戶名,對話資訊
                    AddTalkMessage(receiveString.Substring(splitString[0].Length + 1));
                    break;

                case "talkone":

                    if (CR == null)
                    {
                        CR = new ChattingRom();
                        CR.AddTalkMessage(splitString[1] + splitString[2]);
                        CR.SetText(splitString[1]);
                        CR.talker    = splitString[1];
                        CR.chatting += new Chatting(chatting);
                        Application.Run(CR);
                    }
                    else
                    {
                        CR.rtf_MessageInfo.AppendText(splitString[1] + splitString[2]);
                        CR.AddTalkMessage(splitString[1] + splitString[2]);
                        CR.chatting += new Chatting(chatting);
                        //   Application.Run(CR);
                    }
                    //
                    break;

                case "talkall":        //格式: talkAll,計件者,資訊
                    AddTalkMessage(receiveString.Substring(splitString[0].Length + splitString[1].Length + 2));
                    break;

                case "formatone":
                    CR.SetFont(receiveString);
                    break;

                case "format":
                case "formatall":
                    SetFont(receiveString);
                    break;

                case "allready":
                    gameisOpen      = true;
                    button3.Enabled = false;
                    killMessageBox();
                    break;

                case "startgame":

                    if (splitString[4] == "true")
                    {
                        max = int.Parse(splitString[2]);
                        min = int.Parse(splitString[3]);
                    }
                    else
                    {
                        max = 100;
                        min = 0;
                    }
                    Game a = new Game(splitString[1], min.ToString(), max.ToString(), textBox1.Text);
                    a.finish += new CheckFinish(checkfinish);
                    a.leave  += new CancelGame(cancelGame);
                    Application.Run(a);
                    break;

                case "gameisover":
                    Form.CheckForIllegalCrossThreadCalls = false;
                    button2.Enabled = true;
                    button3.Enabled = true;
                    button4.Enabled = false;
                    break;
                }
            }
            if (isExit == true)
            {
                MessageBox.Show("登出成功");
            }
            Application.Exit();
        }