private void NotifyAllOnlineUsersAboutNewlyJoinedUser(string connecting_user) { // Inform all of the clients of the new message List <string> callbackKeys = loggedInUsers?.Keys.ToList(); // Loops through each logged in user foreach (string key in callbackKeys) { if (key != connecting_user) { try { IOneToOneChatCallback callback = loggedInUsers[key]; callback.SendNewConnectedUserNameToAllOnlineUsers(connecting_user); Console.WriteLine($"Notifying user {key} about joining of {connecting_user}"); } // catches an issue with a user disconnect and loggs off that user 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); // Remove the disconnected client LeaveServer(key); } } } // end of foreach }
public bool AddMeToServer(string userName) { if (userName.Length > 15) { userName = userName.Substring(0, 15); } IOneToOneChatCallback callback = OperationContext.Current.GetCallbackChannel <IOneToOneChatCallback>(); if (!loggedInUsers.ContainsKey(userName)) { loggedInUsers.Add(userName, callback); Console.WriteLine($"User {userName} logged in..."); // Notify all the online users about newly joined user NotifyAllOnlineUsersAboutNewlyJoinedUser(userName); return(true); } // end of if else { DuplicateUserFault fault = new DuplicateUserFault() { Reason = "User '" + userName + "' already logged in!" }; throw new FaultException <DuplicateUserFault>(fault); } // end of else }
} // end of method private void SendMessageToParticularClient(SingleChatMessage chatmessage, string sender, string intendedReceiver) { // Inform all of the clients of the new message List <string> callbackKeys = loggedInUsers?.Keys.ToList(); // Loops through each logged in user foreach (string key in callbackKeys) { if (key == intendedReceiver || key == sender) { try { IOneToOneChatCallback callback = loggedInUsers[key]; callback.SendMessageToParticularClient(chatmessage); Console.WriteLine($"Sending user {key} message {chatmessage}"); } // catches an issue with a user disconnect and loggs off that user 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); // Remove the disconnected client LeaveServer(key); } } } // end of foreach }