private void Connect() { if (proxy == null) { try { this.localClient = new ChatUser(); this.localClient.UserName = loginTxtBoxUName.Text.ToString(); InstanceContext context = new InstanceContext(this); proxy = new Duplex_ChatServiceClient(context); proxy.Open(); proxy.InnerDuplexChannel.Faulted += new EventHandler(InnerDuplexChannel_Faulted); proxy.InnerDuplexChannel.Opened += new EventHandler(InnerDuplexChannel_Opened); proxy.InnerDuplexChannel.Closed += new EventHandler(InnerDuplexChannel_Closed); proxy.ConnectAsync(this.localClient); proxy.ConnectCompleted += new EventHandler <ConnectCompletedEventArgs>(proxy_ConnectCopleted); } catch (Exception exception) { loginTxtBoxUName.Text = exception.Message.ToString(); loginLabelStatus.Content = "Offline"; loginButtonConnect.IsEnabled = true; } } else { HandleProxy(); } }
private void buttonConnect_Click(object sender, RoutedEventArgs e) { loginButtonConnect.IsEnabled = false; loginLabelStatus.Content = "Connecting.."; proxy = null; Connect(); }
private void HandleProxy() { if (proxy != null) { switch (this.proxy.State) { case CommunicationState.Closed: { proxy = null; chatListBoxMsgs.Items.Clear(); chatListBoxNames.Items.Clear(); loginLabelStatus.Content = "Disconnected"; ShowChat(false); ShowLogin(true); loginButtonConnect.IsEnabled = true; break; } case CommunicationState.Closing: break; case CommunicationState.Created: break; case CommunicationState.Faulted: { proxy.Abort(); proxy = null; chatListBoxMsgs.Items.Clear(); chatListBoxNames.Items.Clear(); loginLabelStatus.Content = "Disconnected"; ShowChat(false); ShowLogin(true); loginButtonConnect.IsEnabled = true; break; } case CommunicationState.Opened: { ShowChat(true); ShowLogin(false); chatLabelCurrentStatus.Content = "Online"; chatLabelCurrentUName.Content = this.localClient.UserName; break; } case CommunicationState.Opening: break; default: break; } } }