//This method is to populate/join room public Task LeaveRoom(Guid roomId, int moduleId) { var crc = new ConnectionRecordController(); var crrc = new ConnectionRecordRoomController(); var rc = new RoomController(); var c = crc.GetConnectionRecordByConnectionId(Context.ConnectionId) ?? SetupConnectionRecord(); //lookup client room connection record, if there don't add var connectionRoom = crrc.GetConnectionRecordRoomByConnectionRecordId(c.ConnectionRecordId, roomId); if (connectionRoom != null) { connectionRoom.DepartedDate = DateTime.UtcNow; crrc.UpdateConnectionRecordRoom(connectionRoom); var removeUser = Users.Find(conRec => (conRec.Id == connectionRoom.Id)); Users.Remove(removeUser); } //Remove the user from the SignalR Group (broadcast) Groups.Remove(Context.ConnectionId, roomId.ToString()); return(Clients.Group(roomId.ToString()).updateUserList(Users.FindAll(cc => (cc.RoomId == roomId)), roomId)); }
//TODO: on connection, reload rooms for user? public Task Join() { int moduleId = Convert.ToInt32(Clients.Caller.moduleid); var settingsDefault = new Guid(Clients.Caller.defaultRoomId); if (DefaultRoomId != settingsDefault) { DefaultRoomId = settingsDefault; } //get list of previously connected (not departed) rooms var crrc = new ConnectionRecordRoomController(); var rc = new RoomController(); IEnumerable <Room> myRooms = null; //don't do this if we've got a private room loaded. if (Convert.ToInt32(Clients.Caller.userid) > 0) { myRooms = crrc.GetConnectionRecordRoomsByUserId((int)Clients.Caller.userid); } //TODO: the default room doesn't have a moduleid associated with it //if myRooms is empty, what to do (pass default room) if (myRooms == null) { //load the default room var r = rc.GetRoom(DefaultRoomId, moduleId); myRooms = new List <Room>(); myRooms = myRooms.Concat(new[] { r }); } else { //load the current default room to see if it is in the queue var r = rc.GetRoom(DefaultRoomId, moduleId); if (!myRooms.Contains(r)) { myRooms = myRooms.Concat(new[] { r }); } } //get all the active rooms and send it back for the Lobby var allRooms = rc.GetRooms(moduleId); //we are passing in a list of All rooms, and the current user's rooms Clients.Caller.PopulateUser(allRooms, myRooms); return(base.OnConnected()); }
private void DisconnectUser(string connectionId) { if (connectionId == null) { return; } //TODO: remove user from all rooms var crc = new ConnectionRecordController(); var crrc = new ConnectionRecordRoomController(); var id = connectionId; var cr = crc.GetConnectionRecordByConnectionId(id); if (cr != null) { var roomList = Users.FindAll(c => (c.ConnectionId == connectionId)); //disconnect from each room the user was connected to foreach (UserListRecords rr in roomList) { Users.Remove(rr); Clients.Group(rr.RoomId.ToString()).newMessageNoParse(new Message { AuthorName = Localization.GetString("SystemName.Text", "~/desktopmodules/DnnChat/app_localresources/" + Localization.LocalSharedResourceFile) , AuthorUserId = -1 , ConnectionId = "0", MessageDate = DateTime.UtcNow , MessageId = -1 , MessageText = string.Format(Localization.GetString("Disconnected.Text", "~/desktopmodules/DnnChat/app_localresources/" + Localization.LocalSharedResourceFile) , cr.UserName) , RoomId = rr.RoomId }); //Clients.Group(rr.RoomId.ToString()).updateUserList(Users.FindAll(c => (c.RoomId == rr.RoomId))); Clients.Group(rr.RoomId.ToString()).updateUserList(Users.FindAll(uc => (uc.RoomId == rr.RoomId)), rr.RoomId); } //disconnect the connectionrecord cr.DisConnectedDate = DateTime.UtcNow; crc.UpdateConnectionRecord(cr); } }
/* * When a user connects we need to populate their user information, we default the username to be Anonymous + a # */ //This method is to populate/join room public Task JoinRoom(Guid roomId, int moduleId) { //TODO: don't allow connecting to the same room twice var crc = new ConnectionRecordController(); var crrc = new ConnectionRecordRoomController(); var rc = new RoomController(); var r = rc.GetRoom(roomId, moduleId); if (r.Enabled) { if (r.Private) { //check the password } var c = crc.GetConnectionRecordByConnectionId(Context.ConnectionId) ?? SetupConnectionRecord(); var cr = crrc.GetConnectionRecordRoomByConnectionRecordId(c.ConnectionRecordId, roomId); if (cr == null) { var crr = new ConnectionRecordRoom { ConnectionRecordId = c.ConnectionRecordId, JoinDate = DateTime.UtcNow, DepartedDate = null, RoomId = roomId }; //join the room crrc.CreateConnectionRecordRoom(crr); var ulr = new UserListRecords(c, crr); //add the user to the List of users that will be later filtered by RoomId Users.Add(ulr); } Groups.Add(Context.ConnectionId, roomId.ToString()); //populate history for all previous rooms RestoreHistory(roomId); //lookup the Room to get the Welcome Message Clients.Caller.newMessageNoParse(new Message { AuthorName = Localization.GetString("SystemName.Text", "~/desktopmodules/DnnChat/app_localresources/" + Localization.LocalSharedResourceFile), ConnectionId = "0", MessageDate = DateTime.UtcNow, MessageId = -1, MessageText = r.RoomWelcome, AuthorUserId = -1, RoomId = roomId }); Clients.Group(roomId.ToString()).newMessageNoParse(new Message { AuthorName = Localization.GetString("SystemName.Text", "~/desktopmodules/DnnChat/app_localresources/" + Localization.LocalSharedResourceFile) , AuthorUserId = -1 , ConnectionId = "0", MessageDate = DateTime.UtcNow, MessageId = -1, MessageText = string.Format(Localization.GetString("Connected.Text", "~/desktopmodules/DnnChat/app_localresources/" + Localization.LocalSharedResourceFile), c.UserName), RoomId = roomId }); Clients.Caller.scrollBottom(r.RoomId); return(Clients.Group(roomId.ToString()).updateUserList(Users.FindAll(uc => (uc.RoomId == r.RoomId)), roomId)); } else { //if the room was no longer enabled, return nothing return(null); } }