private ChatMessage GetChatMessage(Cerebello.Model.ChatMessage chatMessage, string clientGuid) { return(new ChatMessage() { Message = chatMessage.Message, UserFrom = this.GetChatUserFromUser(chatMessage.UserFrom), UserTo = this.GetChatUserFromUser(chatMessage.UserTo), ClientGuid = clientGuid }); }
/// <summary> /// Sends a message to a particular user /// </summary> public void SendMessage(int otherUserId, string message, string clientGuid) { var myUserId = this.GetMyUserId(); var myUser = this.db.Users.FirstOrDefault(u => u.Id == myUserId); var otherUser = this.db.Users.FirstOrDefault(u => u.Id == otherUserId); if (myUser == null || otherUser == null) { return; } var dbChatMessage = new Cerebello.Model.ChatMessage() { Date = DateTime.UtcNow, Message = message, UserFromId = myUserId, UserToId = otherUserId, PracticeId = myUser.PracticeId }; this.db.ChatMessages.AddObject(dbChatMessage); this.db.SaveChanges(); var myRoomId = this.GetMyRoomId(); var connectionIds = new List <string>(); lock (connections) { if (connections[myRoomId].ContainsKey(otherUserId)) { connectionIds.AddRange(connections[myRoomId][otherUserId]); } if (connections[myRoomId].ContainsKey(myUserId)) { connectionIds.AddRange(connections[myRoomId][myUserId]); } } foreach (var connectionId in connectionIds) { this.Clients.Client(connectionId).sendMessage(this.GetChatMessage(dbChatMessage, clientGuid)); } }
/// <summary> /// Deprecated Method for adding a new object to the ChatMessages EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToChatMessages(ChatMessage chatMessage) { base.AddObject("ChatMessages", chatMessage); }
/// <summary> /// Create a new ChatMessage object. /// </summary> /// <param name="id">Initial value of the Id property.</param> /// <param name="userFromId">Initial value of the UserFromId property.</param> /// <param name="userToId">Initial value of the UserToId property.</param> /// <param name="practiceId">Initial value of the PracticeId property.</param> /// <param name="date">Initial value of the Date property.</param> /// <param name="message">Initial value of the Message property.</param> public static ChatMessage CreateChatMessage(global::System.Int32 id, global::System.Int32 userFromId, global::System.Int32 userToId, global::System.Int32 practiceId, global::System.DateTime date, global::System.String message) { ChatMessage chatMessage = new ChatMessage(); chatMessage.Id = id; chatMessage.UserFromId = userFromId; chatMessage.UserToId = userToId; chatMessage.PracticeId = practiceId; chatMessage.Date = date; chatMessage.Message = message; return chatMessage; }
/// <summary> /// Sends a message to a particular user /// </summary> public void SendMessage(int otherUserId, string message, string clientGuid) { var myUserId = this.GetMyUserId(); var myUser = this.db.Users.FirstOrDefault(u => u.Id == myUserId); var otherUser = this.db.Users.FirstOrDefault(u => u.Id == otherUserId); if (myUser == null || otherUser == null) return; var dbChatMessage = new Cerebello.Model.ChatMessage() { Date = DateTime.UtcNow, Message = message, UserFromId = myUserId, UserToId = otherUserId, PracticeId = myUser.PracticeId }; this.db.ChatMessages.AddObject(dbChatMessage); this.db.SaveChanges(); var myRoomId = this.GetMyRoomId(); var connectionIds = new List<string>(); lock (connections) { if (connections[myRoomId].ContainsKey(otherUserId)) connectionIds.AddRange(connections[myRoomId][otherUserId]); if (connections[myRoomId].ContainsKey(myUserId)) connectionIds.AddRange(connections[myRoomId][myUserId]); } foreach (var connectionId in connectionIds) this.Clients.Client(connectionId).sendMessage(this.GetChatMessage(dbChatMessage, clientGuid)); }