分享WCF聊天程序--WCFChat实现代码

2019-12-30 11:02:31于海丽
  • ExitChatSession();  Application.Exit(); 
  • }  /// <summary> 
  • /// 群聊  /// </summary> 
  • private void btnChat_Click(object sender, EventArgs e)  { 
  • SayAndClear("", txtChatContent.Text, false);  txtChatContent.Focus(); 
  • }  /// <summary> 
  • /// 发送消息  /// </summary> 
  • private void SayAndClear(string to, string msg, bool pvt)  { 
  • if (msg != "")  { 
  • try  { 
  • CommunicationState cs = proxy.State;  //pvt 公聊还是私聊 
  • if (!pvt)  { 
  • proxy.Say(msg);  } 
  • else  { 
  • proxy.Whisper(to, msg);  } 
  •   txtChatContent.Text = ""; 
  • }  catch 
  • {  AbortProxyAndUpdateUI(); 
  • AppendText("失去连接: " + DateTime.Now.ToString() + Environment.NewLine);  ExitChatSession(); 
  • }  } 
  • }  private void txtChatContent_KeyPress(object sender, KeyPressEventArgs e) 
  • {  if (e.KeyChar == 13) 
  • {  e.Handled = true; 
  • btnChat.PerformClick();  } 
  • }  /// <summary> 
  • /// 只有选择一个用户时,私聊按钮才可用  /// </summary> 
  • private void lbOnlineUsers_SelectedIndexChanged(object sender, EventArgs e)  { 
  • AdjustWhisperButton();  } 
  • /// <summary>  /// 私聊 
  • /// </summary>   private void btnWhisper_Click(object sender, EventArgs e) 
  • {  if (txtChatDetails.Text == "") 
  • {  return; 
  • }  object to = lbOnlineUsers.SelectedItem; 
  • if (to != null)  { 
  • string receiverName = (string)to;  AppendText("私下对" + receiverName + "说: " + txtChatContent.Text);//+ Environment.NewLine