private void TXBinput_TextChanged(object sender, EventArgs e) { var text = (sender as MyTextBox).Text; if (text.Length == 0 || !text.EndsWith("\r\n")) { return; } var idx = text.IndexOf("\r\n"); (sender as MyTextBox).Text = text.Substring(idx + 2); text = text.Remove(idx); if (text.StartsWith("/") || State != StateEnum.Chatting) { if (text.StartsWith("/c")) { ButtonClicked(BTNconnect, null); } else if (text.StartsWith("/t")) { ButtonClicked(BTNmatch, null); } else if (text.StartsWith("/q")) { ButtonClicked(BTNquit, null); } else { Msg($"無此指令:{text}"); } } else { text = Encode(text); var msg = JsonConvert.SerializeObject(new Json.SendMessage { message = text, sequence = Rand.Next(int.MaxValue) }); lock (manager.syncRoot) { manager.Send(msg); while ((msg = manager.Receive()) == null) { Application.DoEvents(); } Trace.Assert(JsonConvert.DeserializeObject <Json.SendMessage>(msg).message == text); Log($"你傳了:{text}"); TXBchat.AppendText($"你傳了:{Decode(text)}\r\n"); } } }
private async void ButtonClicked(object sender, EventArgs e) { if (!(sender as MyButton).Enabled) { Msg($"沒有作用的指令:{(sender as MyButton).Text}"); return; } switch ((sender as MyButton).Text) { case "/c (連線至伺服器)": { try { if (State != StateEnum.Disconnected) { return; } Msg($"正在連線...ip={info.ip.Text},port={info.port.Text}"); manager.Connect(info.ip.Text, int.Parse(info.port.Text)); Msg($"連線成功!"); SetState(StateEnum.Idle); } catch (Exception error) { Show(error.ToString()); } } break; case "/c (結束網路連線)": { try { if (State == StateEnum.Disconnected) { return; } Msg($"正在關閉連線..."); manager.Disconnect(); Msg($"已成功關閉連線!"); SetState(StateEnum.Disconnected); } catch (Exception error) { Show(error.ToString()); } } break; case "/t (嘗試匹配)": { try { if (State != StateEnum.Idle) { return; } var msg = JsonConvert.SerializeObject(new Json.TryMatch { name = info.name.Text, age = int.Parse(info.age.Text), gender = info.gender.Text, introduction = info.introduction.Text, filter_function = info.filter_function.Text }); Send(msg); while (Receive() == null) { await Task.Delay(500); Application.DoEvents(); } Msg("匹配中..."); SetState(StateEnum.Matching); while (State == StateEnum.Matching) { Application.DoEvents(); await Task.Delay(500); if ((msg = Receive()) != null) { Msg("成功匹配"); var partner = JsonConvert.DeserializeObject <Json.Matched>(msg); TXBchat.AppendText($"對方資訊:\r\n"); TXBchat.AppendText($"暱稱:{partner.name}\r\n"); TXBchat.AppendText($"年齡:{partner.age}\r\n"); TXBchat.AppendText($"性別:{partner.gender}\r\n"); TXBchat.AppendText($"自介:{partner.introduction}\r\n"); SetState(StateEnum.Chatting); } } while (State == StateEnum.Chatting) { Application.DoEvents(); await Task.Delay(500); if ((msg = Receive()) != null) { var msgObj = JsonConvert.DeserializeObject <Json.ReceiveMessage>(msg); if (msgObj.cmd == "other_side_quit") { Msg($"對方已離開"); SetState(StateEnum.Idle); } else { Log($"對方回覆:{msgObj.message}"); TXBchat.AppendText($"對方回覆:{Decode(msgObj.message)}\r\n"); } } } } catch (Exception error) { Show(error.ToString()); } } break; case "/q (放棄匹配)": { try { if (State != StateEnum.Matching) { return; } var msg = JsonConvert.SerializeObject(new Json.Quit()); lock (manager.syncRoot) { manager.Send(msg); while ((msg = manager.Receive()) == null) { Application.DoEvents(); } Trace.Assert(JsonConvert.DeserializeObject <Json.Quit>(msg).cmd == "quit"); SetState(StateEnum.Idle); } } catch (Exception error) { Show(error.ToString()); } } break; case "/q (結束聊天)": { try { if (State != StateEnum.Chatting) { return; } var msg = JsonConvert.SerializeObject(new Json.Quit()); lock (manager.syncRoot) { manager.Send(msg); while ((msg = manager.Receive()) == null) { Application.DoEvents(); } Trace.Assert(JsonConvert.DeserializeObject <Json.Quit>(msg).cmd == "quit"); SetState(StateEnum.Idle); } } catch (Exception error) { Show(error.ToString()); } } break; } }
void SetState(StateEnum state) { if (State == StateEnum.Chatting && state != StateEnum.Chatting) { TXBchat.AppendText("==========對話已結束==========\r\n"); } else if (State != StateEnum.Chatting && state == StateEnum.Chatting) { TXBchat.AppendText("現在開始聊天吧!\r\n"); } State = state; switch (state) { case StateEnum.Disconnected: { var text = new string[3] { "/c (連線至伺服器)", "/t (沒有作用)", "/q (沒有作用)" }; var enabled = new bool[3] { true, false, false }; BTNconnect.Text = text[0]; BTNmatch.Text = text[1]; BTNquit.Text = text[2]; BTNconnect.Enabled = enabled[0]; BTNmatch.Enabled = enabled[1]; BTNquit.Enabled = enabled[2]; } break; case StateEnum.Idle: { if (!firstIdle) { firstIdle = true; } else { Msg($"回到閒置狀態"); } var text = new string[3] { "/c (結束網路連線)", "/t (嘗試匹配)", "/q (沒有作用)" }; var enabled = new bool[3] { true, true, false }; BTNconnect.Text = text[0]; BTNmatch.Text = text[1]; BTNquit.Text = text[2]; BTNconnect.Enabled = enabled[0]; BTNmatch.Enabled = enabled[1]; BTNquit.Enabled = enabled[2]; } break; case StateEnum.Matching: { var text = new string[3] { "/c (結束網路連線)", "/t (沒有作用)", "/q (放棄匹配)" }; var enabled = new bool[3] { true, false, true }; BTNconnect.Text = text[0]; BTNmatch.Text = text[1]; BTNquit.Text = text[2]; BTNconnect.Enabled = enabled[0]; BTNmatch.Enabled = enabled[1]; BTNquit.Enabled = enabled[2]; } break; case StateEnum.Chatting: { var text = new string[3] { "/c (結束網路連線)", "/t (沒有作用)", "/q (結束聊天)" }; var enabled = new bool[3] { true, false, true }; BTNconnect.Text = text[0]; BTNmatch.Text = text[1]; BTNquit.Text = text[2]; BTNconnect.Enabled = enabled[0]; BTNmatch.Enabled = enabled[1]; BTNquit.Enabled = enabled[2]; } break; } }