//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)); }
private ConnectionRecord SetupConnectionRecord() { string username = Clients.Caller.username; //if (string.IsNullOrEmpty(username)) //{ // Clients.Caller.newMessageNoParse(new Message { AuthorName = Localization.GetString("SystemName.Text", "~/desktopmodules/DnnChat/app_localresources/" + Localization.LocalSharedResourceFile), ConnectionId = "0", MessageDate = DateTime.UtcNow, MessageId = -1, MessageText = string.Format(Localization.GetString("BadConnection.Text", "~/desktopmodules/DnnChat/app_localresources/" + Localization.LocalSharedResourceFile), "phantom") }); // return new ConnectionRecord(); //} if (username.Trim() == "phantom" || username.Trim() == string.Empty) { username = string.Format(Localization.GetString("AnonymousUser.Text", "~/desktopmodules/DnnChat/app_localresources/" + Localization.LocalSharedResourceFile), (Users.Count + 1)); } Clients.Caller.username = username; var userId = -1; if (Convert.ToInt32(Clients.Caller.userid) > 0) { userId = Convert.ToInt32(Clients.Caller.userid); } //check if the connectionrecord is already in the DB var crc = new ConnectionRecordController(); var c = crc.GetConnectionRecordByConnectionId(Context.ConnectionId); if (c != null) { c.UserName = username; crc.UpdateConnectionRecord(c); } else { c = new ConnectionRecord { ConnectionId = Context.ConnectionId, ConnectedDate = DateTime.UtcNow, ModuleId = Convert.ToInt32(Clients.Caller.moduleid), UserName = username, UserId = userId, IpAddress = GetIpAddress() }; //Users.Add(c); crc.CreateConnectionRecord(c); } //store the record for the connection return(c); }
/* * This method gets called when someone updates their name. We need to store the change in the ConnectionRecord * We also want to send that info back to the client's state to update it there. */ public string UpdateName(string userName) { var crc = new ConnectionRecordController(); var id = Context.ConnectionId; var cr = crc.GetConnectionRecordByConnectionId(id); var crId = cr.ConnectionRecordId; //we need to remove the original CR before updating it in the users list //TODO: this isn't the right search var roomList = Users.FindAll(c => (c.ConnectionRecordId == crId)); foreach (UserListRecords rr in roomList) { //todo: it doesn't look like the Remove happens on the client side Users.Remove(rr); var originalName = rr.UserName; //set the new name on both record objects rr.UserName = cr.UserName = userName; //we need to update with the connectionrecord not UserListRecord crc.UpdateConnectionRecord(cr); Users.Add(rr); var nameChange = String.Format(Localization.GetString("NameChange.Text", "~/desktopmodules/DnnChat/app_localresources/" + Localization.LocalSharedResourceFile), originalName, rr.UserName); Clients.Group(rr.RoomId.ToString()).updateUserList(Users.FindAll(uc => (uc.RoomId == rr.RoomId)), rr.RoomId); var m = new Message { ConnectionId = Context.ConnectionId, MessageDate = DateTime.UtcNow, MessageText = nameChange, AuthorName = originalName, AuthorUserId = -1, ModuleId = rr.ModuleId, RoomId = rr.RoomId }; new MessageController().CreateMessage(m); Clients.Group(rr.RoomId.ToString()).newMessage(m); //update message for all rooms } Clients.Caller.updateName(userName); //else return nothing return(string.Empty); }
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); } }
public void Send(string message, Guid roomId) { //if no valid connectionrecord don't let the message go through var crc = new ConnectionRecordController(); var cr = crc.GetConnectionRecordByConnectionId(Context.ConnectionId); //if the user (connectionrecord) isn't in a room don't let message go through var rc = new RoomController(); if (cr != null && rc.UserInRoom(roomId, cr)) { // parse message before use if (Clients.Caller.username != null && Clients.Caller.username.Trim() != "phantom") { var parsedMessage = ParseMessage(message, roomId); if (parsedMessage != string.Empty) { int moduleId; int authorUserId; //int.TryParse(Clients.Caller.moduleid, out moduleId); moduleId = Convert.ToInt32(Clients.Caller.moduleid); authorUserId = Convert.ToInt32(Clients.Caller.userid); var m = new Message { ConnectionId = Context.ConnectionId, MessageDate = DateTime.UtcNow, MessageText = parsedMessage, AuthorName = Clients.Caller.username, AuthorUserId = authorUserId, ModuleId = moduleId, RoomId = roomId }; new MessageController().CreateMessage(m); Clients.Group(roomId.ToString()).newMessage(m); } } else { // if there is no username for the user don't let them post var m = new Message { ConnectionId = Context.ConnectionId, MessageDate = DateTime.UtcNow, MessageText = Localization.GetString("FailedUnknown.Text", "~/desktopmodules/DnnChat/app_localresources/" + Localization.LocalSharedResourceFile), AuthorName = Localization.GetString("SystemName.Text", "~/desktopmodules/DnnChat/app_localresources/" + Localization.LocalSharedResourceFile), AuthorUserId = -1, RoomId = DefaultRoomId }; Clients.Caller.newMessage(m); } } else { // if there is no username for the user don't let them post var m = new Message { ConnectionId = Context.ConnectionId, MessageDate = DateTime.UtcNow, MessageText = Localization.GetString("FailedUnknown.Text", "~/desktopmodules/DnnChat/app_localresources/" + Localization.LocalSharedResourceFile), AuthorName = Localization.GetString("SystemName.Text", "~/desktopmodules/DnnChat/app_localresources/" + Localization.LocalSharedResourceFile), AuthorUserId = -1, RoomId = DefaultRoomId }; Clients.Caller.newMessage(m); } }
/* * 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); } }