/// <summary> /// This method is used to get user list from chat server. /// It is called by server, once after user logged in to server. /// </summary> /// <param name="users">All online user informations</param> public void GetUserList(UserInfo[] users) { foreach (var user in users) { _chatRoom.AddUserToList(user); } }
/// <summary> /// Used to login to chat service. /// </summary> /// <param name="userInfo">User informations</param> public void Login(UserInfo userInfo) { //Check nick if it is being used by another user if (FindClientByNick(userInfo.Nick) != null) { throw new NickInUseException("The nick '" + userInfo.Nick + "' is being used by another user. Please select another one."); } //Get a reference to the current client that is calling this method var client = CurrentClient; //Get a proxy object to call methods of client when needed var clientProxy = client.GetClientProxy<IChatClient>(); //Create a ChatClient and store it in a collection var chatClient = new ChatClient(client, clientProxy, userInfo); _clients[client.ClientId] = chatClient; //Register to Disconnected event to know when user connection is closed client.Disconnected += Client_Disconnected; //Start a new task to send user list to new user and to inform //all users that a new user joined to room Task.Factory.StartNew( () => { OnUserListChanged(); SendUserListToClient(chatClient); SendUserLoginInfoToAllClients(userInfo); }); }
/// <summary> /// Creates a new ChatClient object. /// </summary> /// <param name="client">Scs client reference</param> /// <param name="clientProxy">Proxy object to call remote methods of chat client</param> /// <param name="userInfo">User informations of client</param> public ChatClient(IScsServiceClient client, IChatClient clientProxy, UserInfo userInfo) { Client = client; ClientProxy = clientProxy; User = userInfo; }
/// <summary> /// This method is used to inform all online clients /// that a new user joined to room. /// </summary> /// <param name="userInfo">New joined user's informations</param> private void SendUserLoginInfoToAllClients(UserInfo userInfo) { foreach (var client in _clients.GetAllItems()) { //Do not send informations to user that is logged in. if (client.User.Nick == userInfo.Nick) { continue; } try { client.ClientProxy.OnUserLogin(userInfo); } catch { } } }
/// <summary> /// Adds user to user list in right area of the window. /// </summary> /// <param name="userInfo">New user informations</param> private void AddUserToListInternal(UserInfo userInfo) { //Do not add the current user (that is using the application) to user list if (userInfo.Nick == CurrentUserInfo.Nick) { return; } //Do not add user to list if it is already exists. if (FindUserInList(userInfo.Nick) != null) { return; } //Find correct order (by name) to insert the user var orderedIndex = 0; foreach (UserCardControl userCardControl in spUsers.Children) { if (userInfo.Nick.CompareTo(userCardControl.UserNick) < 0) { break; } orderedIndex++; } //Create user control var userCard = new UserCardControl { UserNick = userInfo.Nick, UserStatus = userInfo.Status, AvatarBytes = userInfo.AvatarBytes, Height = 60 }; userCard.MouseDoubleClick += UserCard_MouseDoubleClick; //Insert user to user list spUsers.Children.Insert( orderedIndex, userCard ); //Enable private messaging window if any open with that user if (_privateChatWindows.ContainsKey(userInfo.Nick)) { _privateChatWindows[userInfo.Nick].UserLoggedIn(); _privateChatWindows[userInfo.Nick].RemoteUserStatus = userInfo.Status; _privateChatWindows[userInfo.Nick].RemoteUserAvatar = userCard.AvatarImageSource; } }
/// <summary> /// This methos is used to add a new user to user list in room view. /// </summary> /// <param name="userInfo">Informations of new user</param> public void AddUserToList(UserInfo userInfo) { Dispatcher.Invoke(new Action(() => AddUserToListInternal(userInfo))); }
/// <summary> /// This method is called from chat server to inform that a new user /// joined to chat room. /// </summary> /// <param name="userInfo">Informations of new user</param> public void OnUserLogin(UserInfo userInfo) { _chatRoom.AddUserToList(userInfo); }