/// <summary> /// Adds a message to the room /// <param name="msg"></param> /// <returns> the id of the new message</returns> public int AddMsg(Message msg) { int count; lock (messages) { count = messages.Count; messages.Add(msg); } return count; }
/// <summary> /// Marks the user as inactive /// </summary> /// <param name="userID"></param> /// <returns></returns> public void LeaveRoom(string userID) { //deactivate user ChatUser user = this.GetUser(userID); if (user == null) return; user.IsActive = false; user.LastSeen = DateTime.Now; this.RoomUsers.Remove(userID); //Add leaving message Message msg = new Message(user.UserName, "", MsgType.Left); this.AddMsg(msg); if (IsEmpty()) ChatEngine.DeleteRoom(this.RoomID); }
/// <summary> /// Adds a message in the room /// </summary> /// <param name="strMsg"></param> /// <param name="senderID"></param> /// <returns>All the messages sent from the other user from the last time the user sent a message</returns> public string SendMessage(string strMsg, string senderID) { ChatUser user = this.GetUser(senderID); Message msg = new Message(user.UserName, strMsg, MsgType.Msg); user.LastSeen = DateTime.Now; this.ExpireUsers(userChatRoomSessionTimeout); this.AddMsg(msg); int lastMsgID; List<Message> previousMsgs = this.GetMessagesSince(user.LastMessageReceived, out lastMsgID); if (lastMsgID != -1) user.LastMessageReceived = lastMsgID; string res = this.GenerateMessagesString(previousMsgs); return res; }
/// <summary> /// Activates the user and adds a join message to the room /// </summary> /// <param name="userID"></param> /// <param name="userName"></param> /// <returns>All the messages sent in the room</returns> public string JoinRoom(string userID, string userName) { //activate user ChatUser user = new ChatUser(userID, userName); user.IsActive = true; user.UserName = userName; user.LastSeen = DateTime.Now; if (!this.RoomUsers.ContainsKey(userID)) { //Add join message Message msg = new Message(user.UserName, "", MsgType.Join); this.AddMsg(msg); //Get all the messages to the user int lastMsgID; List<Message> previousMessages = this.GetMessagesSince(-1, out lastMsgID); user.LastMessageReceived = lastMsgID; //return the messages to the user string str = GenerateMessagesString(previousMessages); this.RoomUsers.Add(userID, user); return str; } return ""; }