public void Join(bool reconnecting) { // Get the client state var userId = Context.User.GetUserId(); // Try to get the user from the client state ChatUser user = _repository.GetUserById(userId); if (reconnecting) { _logger.Log("{0}:{1} connected after dropping connection.", user.Name, Context.ConnectionId); // If the user was marked as offline then mark them inactive if (user.Status == (int)UserStatus.Offline) { user.Status = (int)UserStatus.Inactive; _repository.CommitChanges(); } // Ensure the client is re-added _service.AddClient(user, Context.ConnectionId, UserAgent); } else { _logger.Log("{0}:{1} connected.", user.Name, Context.ConnectionId); // Update some user values _service.UpdateActivity(user, Context.ConnectionId, UserAgent); _repository.CommitChanges(); } ClientState clientState = GetClientState(); OnUserInitialize(clientState, user, reconnecting); }
public bool Join() { // Set the version on the client Caller.version = typeof(Chat).Assembly.GetName().Version.ToString(); // Get the client state ClientState clientState = GetClientState(); // Try to get the user from the client state ChatUser user = _repository.GetUserById(clientState.UserId); // Threre's no user being tracked if (user == null) { return(false); } // Update some user values _service.AddClient(user, Context.ConnectionId); _service.UpdateActivity(user); _repository.CommitChanges(); OnUserInitialize(clientState, user); return(true); }
public override Task OnReconnected() { var userId = Context.User.Identity.Name; ChatUser user = _repository.VerifyUserId(userId); // Make sure this client is being tracked _service.AddClient(user, Context.ConnectionId, UserAgent); var currentStatus = (UserStatus)user.Status; if (currentStatus == UserStatus.Offline) { // Mark the user as inactive user.Status = (int)UserStatus.Inactive; _repository.CommitChanges(); // If the user was offline that means they are not in the user list so we need to tell // everyone the user is really in the room var userViewModel = new UserViewModel(user); foreach (var room in user.Rooms) { var isOwner = user.OwnedRooms.Contains(room); // Tell the people in this room that you've joined Clients.Group(room.Name).addUser(userViewModel, room.Name, isOwner).Wait(); // Add the caller to the group so they receive messages Groups.Add(Context.ConnectionId, room.Name); } } return(base.OnReconnected()); }
public bool CheckStatus() { bool outOfSync = OutOfSync; SetVersion(); string id = Caller.id; ChatUser user = _repository.VerifyUserId(id); // Make sure this client is being tracked _service.AddClient(user, Context.ConnectionId, UserAgent); var currentStatus = (UserStatus)user.Status; if (currentStatus == UserStatus.Offline) { // Mark the user as inactive user.Status = (int)UserStatus.Inactive; _repository.CommitChanges(); // If the user was offline that means they are not in the user list so we need to tell // everyone the user is really in the room var userViewModel = new UserViewModel(user); foreach (var room in user.Rooms) { var isOwner = user.OwnedRooms.Contains(room); // Tell the people in this room that you've joined Clients[room.Name].addUser(userViewModel, room.Name, isOwner).Wait(); // Update the room count OnRoomChanged(room); // Add the caller to the group so they receive messages GroupManager.AddToGroup(Context.ConnectionId, room.Name).Wait(); } } return(outOfSync); }
public void Join(bool reconnecting) { // Get the client state var userId = Context.User.GetUserId(); // Try to get the user from the client state ChatUser user = _repository.GetUserById(userId); if (reconnecting) { // If the user was marked as offline then mark them inactive if (user.Status == (int)UserStatus.Offline) { user.Status = (int)UserStatus.Inactive; _repository.CommitChanges(); } // Ensure the client is re-added _chatService.AddClient(user, Context.ConnectionId, UserAgent); } else { // Update some user values _chatService.UpdateActivity(user, Context.ConnectionId, UserAgent); _repository.CommitChanges(); } ClientState clientState = GetClientState(); // This function is being manually called here to establish // your identity to SignalR and update the UI to match. In // original JabbR it isn't called explicitly anywhere, so // something about the natural authentication data flow // establishes this in SignalR for us. For now, call explicitly //Delete this in the future (when auth is setup properly) var userViewModel = new UserViewModel(user); Clients.Caller.userNameChanged(userViewModel); OnUserInitialize(clientState, user, reconnecting); }
public Task Reconnect(IEnumerable <string> groups) { string id = GetUserId(); if (String.IsNullOrEmpty(id)) { return(null); } ChatUser user = _repository.VerifyUserId(id); // Make sure this client is being tracked _service.AddClient(user, Context.ConnectionId, UserAgent); var currentStatus = (UserStatus)user.Status; if (currentStatus == UserStatus.Offline) { // Mark the user as inactive user.Status = (int)UserStatus.Inactive; _repository.CommitChanges(); // If the user was offline that means they are not in the user list so we need to tell // everyone the user is really in the room var userViewModel = new UserViewModel(user); foreach (var room in user.Rooms) { var isOwner = user.OwnedRooms.Contains(room); // Tell the people in this room that you've joined Clients[room.Name].addUser(userViewModel, room.Name, isOwner).Wait(); // Add the caller to the group so they receive messages Groups.Add(Context.ConnectionId, room.Name).Wait(); } } return(null); }
/// <summary> /// Used to reserve a nick name. /// /nick nickname - sets the user's name to nick name or creates a user with that name /// /nick nickname password - sets a password for the specified nick name (if the current user has that nick name) /// /nick nickname password newpassword - updates the password for the specified nick name (if the current user has that nick name) /// </summary> private void HandleNick(string[] parts) { if (parts.Length == 1) { throw new InvalidOperationException("No nick specified!"); } string userName = parts[1]; if (String.IsNullOrWhiteSpace(userName)) { throw new InvalidOperationException("No nick specified!"); } string password = null; if (parts.Length > 2) { password = parts[2]; } string newPassword = null; if (parts.Length > 3) { newPassword = parts[3]; } // See if there is a current user ChatUser user = _repository.GetUserById(_userId); if (user == null && String.IsNullOrEmpty(newPassword)) { user = _repository.GetUserByName(userName); // There's a user with the name specified if (user != null) { if (String.IsNullOrEmpty(password)) { ChatService.ThrowUserExists(userName); } else { // If there's no user but there's a password then authenticate the user _chatService.AuthenticateUser(userName, password); // Add this client to the list of clients for this user _chatService.AddClient(user, _clientId); // Initialize the returning user _notificationService.LogOn(user, _clientId); } } else { // If there's no user add a new one user = _chatService.AddUser(userName, _clientId, password); // Notify the user that they're good to go! _notificationService.OnUserCreated(user); } } else { if (String.IsNullOrEmpty(password)) { string oldUserName = user.Name; // Change the user's name _chatService.ChangeUserName(user, userName); _notificationService.OnUserNameChanged(user, oldUserName, userName); } else { // If the user specified a password, verify they own the nick ChatUser targetUser = _repository.VerifyUser(userName); // Make sure the current user and target user are the same if (user != targetUser) { throw new InvalidOperationException("You can't set/change the password for a nickname you down own."); } if (String.IsNullOrEmpty(newPassword)) { if (targetUser.HashedPassword == null) { _chatService.SetUserPassword(user, password); _notificationService.SetPassword(); } else { throw new InvalidOperationException("Use /nick [nickname] [oldpassword] [newpassword] to change and existing password."); } } else { _chatService.ChangeUserPassword(user, password, newPassword); _notificationService.ChangePassword(); } } } // Commit the changes _repository.CommitChanges(); }