Reputation: 6338
I have a web site where many registered users are connected on the same time and they can send messages one to each other. Each user has his own Session object on server and in this Session object I store "Messages" list - this are the User's messages.
Session["Messages"] = new List<UserMesages>();
//.... more code here
Session["Messages"] = BL.GetMyMessages(); //get a list of messages
//.... more code here
BL.MessageReceived += RefreshUserMessages();
public static void RefreshUserMessages(){
//this code doesn't execute for all the sessions, but only for my current user.
}
I need a way to know when the user received a new message - I don't want on each request to interrogate my database.
I tried to use an event in by BL classes (when a message is stored in database, an event is raised), but the events don't pass from one Session to other Session.
In my BL class I have:
public delegate void MessageReceivedEventHandler(object sender, MessageEventArgs e);
public static event MessageReceivedEventHandler MessageReceived;
public static int SaveMessageForUser(Message msg) {
//... save the message
if(MessageReceived != null) {
MessageReceived (userid, msg) // <-- correct parameters go here
}
}
Any idea? How to implement, in ASP.Net such
Upvotes: 4
Views: 114
Reputation: 303
I recommend that you try to rather approach this problem with SignalR. SignalR is the perfect component for building a real-time chat application with minimal effort.
It will allow you to do everything you want to do.
Checkout this tutorial:
http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr
Upvotes: 5
Reputation: 1650
I'd probably approach this using the Application -- add some kind of tracking table or list, or a set of tables or lists depending, to which you add the user ID when they are sent a message from another user, and remove it when the messages are checked. That would void any need to interrogate the database, and should provide a simple means for the system to identify if a message has been received for that user.
Upvotes: 1