/// <summary> /// This is the second important method, which creates /// the proxy, subscribe to connection state events /// and open a connection with the service /// </summary> private void Connect() { if (proxy == null) { try { this.localClient = new SVC.Client(); this.localClient.Name = loginTxtBoxUName.Text.ToString(); this.localClient.AvatarID = loginComboBoxImgs.SelectedIndex; InstanceContext context = new InstanceContext(this); proxy = new SVC.ChatClient(context); //As the address in the configuration file is set to localhost //we want to change it so we can call a service in internal //network, or over internet string servicePath = proxy.Endpoint.ListenUri.AbsolutePath; string serviceListenPort = proxy.Endpoint.Address.Uri.Port.ToString(); proxy.Endpoint.Address = new EndpointAddress("net.tcp://" + loginTxtBoxIP.Text.ToString() + ":" + serviceListenPort + servicePath); 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_ConnectCompleted); } catch (Exception ex) { loginTxtBoxUName.Text = ex.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(); }
/// <summary> /// This is the most method I like, it helps us alot /// We may can't know when a connection is lost in /// of network failure or service stopped. /// And also to maintain performance client doesnt know /// that the connection will be lost when hitting the /// disconnect button, but when a session is terminated /// this method will be called, and it will handle everything. /// </summary> 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(); ShowChat(false); ShowLogin(true); loginLabelStatus.Content = "Disconnected"; loginButtonConnect.IsEnabled = true; break; case CommunicationState.Opened: ShowLogin(false); ShowChat(true); chatLabelCurrentStatus.Content = "online"; chatLabelCurrentUName.Content = this.localClient.Name; Dictionary<int, Image> images = GetImages(); Image img = images[loginComboBoxImgs.SelectedIndex]; chatCurrentImage.Source = img.Source; break; case CommunicationState.Opening: break; default: break; } } }