/// <summary> /// LogOff /// Logs off the user from the service /// and updates the GUI locally /// </summary> private void LogOff() { // Call the Service to Log Off DuplexChannelFactory <IOneToOneChatService> cf = new DuplexChannelFactory <IOneToOneChatService>(this, "NetTcpBinding_IOneToOneChatService"); cf.Open(); IOneToOneChatService proxy = cf.CreateChannel(); if (proxy != null) { try { proxy.LeaveServer(LoggedInUser.Username); // Disable the GUI for Chat UpdateChatGUI(false); } // end of try catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace + "." + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name); MessageBox.Show("Error logging off user: "******"Cannot Create a Channel to a Proxy. Check Your Configuration Settings.", "Proxy", MessageBoxButtons.OK); } // end of else } // end of method
} // end of method private void UpdateOnlineUsersListboxUI() { // If User is Not Logged In if (LoggedInUser.Username == null) { MessageBox.Show("Please Login First !"); return; } // Clear the List Box lbOnlineUsers.Items.Clear(); // Temporary variable to hold the list of chat messages List <string> receivedOnlineUsers; // Retrieve the New Messages DuplexChannelFactory <IOneToOneChatService> cf = new DuplexChannelFactory <IOneToOneChatService>(this, "NetTcpBinding_IOneToOneChatService"); cf.Open(); IOneToOneChatService proxy = cf.CreateChannel(); if (proxy != null) { try { // retrieve the online username from the server receivedOnlineUsers = proxy.GetAllOnlineUsers(LoggedInUser.Username); // Update the UI foreach (string item in receivedOnlineUsers) { lbOnlineUsers.Items.Add(item); } } // end of try catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace + "." + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name); MessageBox.Show(ex.Message); } } // end of if else { // Cannot Connect to Server MessageBox.Show("Cannot Create a Channel to a Proxy. Check Your Configuration Settings.", "Proxy", MessageBoxButtons.OK); } // end of else } // end of method
/// <summary> /// btnSendMessage_Click /// Implements the Send Message Button Click /// </summary> /// <param name="sender">not used</param> /// <param name="e">not used</param> private void btnSendMessage_Click(object sender, EventArgs e) { // Validate User LoggedIn if (txtMyMessage.Enabled == false || LoggedInUser.Username == null) { // User needs to login! MessageBox.Show("Please login before sending a message.", "Login Required", MessageBoxButtons.OK); } else { // Send Message to Service // Retrieve the New Messages using (DuplexChannelFactory <IOneToOneChatService> cf = new DuplexChannelFactory <IOneToOneChatService>(this, "NetTcpBinding_IOneToOneChatService")) { cf.Open(); IOneToOneChatService proxy = cf.CreateChannel(); if (proxy != null) { try { // It is ok to send empty messages, I don't care lol proxy.SendMessageToParticularUser(LoggedInUser.Username, LoggedInUser.receiverName, txtMyMessage.Text); } // end of try catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace + "." + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name); MessageBox.Show(ex.Message); } // end of catch } // end of if else { // Cannot Connect to Server MessageBox.Show("Cannot Create a Channel to a Proxy. Check Your Configuration Settings.", "Proxy", MessageBoxButtons.OK); } // end of else } // end of using } // end of if } // end of method
private void JoinServer(string username) { // Check State on Service // Make a ChannelFactory Proxy to the Service DuplexChannelFactory <IOneToOneChatService> cf = new DuplexChannelFactory <IOneToOneChatService>(this, "NetTcpBinding_IOneToOneChatService"); cf.Open(); IOneToOneChatService proxy = cf.CreateChannel(); if (proxy != null) { try { proxy.AddMeToServer(username); Console.WriteLine("Inside join server"); // Disable the GUI for Chat UpdateChatGUI(true); } // end of try catch (FaultException <DuplicateUserFault> ex) { System.Diagnostics.Debug.WriteLine(ex.Detail.Reason, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace + "." + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name); MessageBox.Show(ex.Detail.Reason); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace + "." + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name); MessageBox.Show("Error logging off user: "******"Cannot Create a Channel to a Proxy. Check Your Configuration Settings.", "Proxy", MessageBoxButtons.OK); } // end of else }
} // end of method /// <summary> /// GetMessageHistory /// Gets a list of ChatMessages to update /// the UI list box /// </summary> private void UpdateMessageHistoryUI(bool multipleTimes) { // If User is Not Logged In if (LoggedInUser.Username == null) { MessageBox.Show("Please Login First !"); return; } // Temporary variable to hold the list of chat messages List <SingleChatMessage> historyChat; // Retrieve the New Messages DuplexChannelFactory <IOneToOneChatService> cf = new DuplexChannelFactory <IOneToOneChatService>(this, "NetTcpBinding_IOneToOneChatService"); cf.Open(); IOneToOneChatService proxy = cf.CreateChannel(); if (proxy != null) { try { IsChatHistoryLoaded = true; if (LoggedInUser.receiverName == LoggedInUser.previousReceiver || multipleTimes) { if (LoggedInUser.receiverName == null) { lstChatMessages.Items.Clear(); lstChatMessages.Items.Add("NO MESSAGE RECORDS"); } } else { lstChatMessages.Items.Clear(); // retrieve the chat history historyChat = proxy.GetMessageHistory(LoggedInUser.Username, LoggedInUser.receiverName); if (historyChat.Count == 0) { lstChatMessages.Items.Add("NO MESSAGE RECORDS"); return; } foreach (SingleChatMessage msg in historyChat) { lstChatMessages.Items.Add(msg); Console.WriteLine("MSG : " + msg); } Console.WriteLine("Sender : " + LoggedInUser.Username + " Receiver : " + LoggedInUser.receiverName); } } // end of try catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace + "." + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name); MessageBox.Show(ex.Message); } } // end of if else { // Cannot Connect to Server MessageBox.Show("Cannot Create a Channel to a Proxy. Check Your Configuration Settings.", "Proxy", MessageBoxButtons.OK); } // end of else } // end of method