(Assuming it's even possible in .Net, of course.)
Ultimately, what I want to acplish is an AJAX-based messaging system. I already have a database table for messages, and an ASPX to add new messages (and declare old messages no longer relevant).
The current messaging system simply polls the server every 15 seconds, and re-pulls the current message set.
What I am looking to do is: On $(document).ready(), register an ajax callback function that listens for a server-side event (e.g., MessagesUpdated) On table insert/update, fire MessagesUpdated server-side.
This way, whenever the table is updated (or new records added), any clients listening know that new data is available and can re-poll the server then.
Ideally, I'd also like to make the new data available as an event argument (to minimize re-polling the db).
I can find references to something like this in other languages, but I cannot find any actual code examples to get me started.
Assuming this is possible to do via .Net, can anyone help get me started on this?
I'm using the 2.0 Framework. Also while I added a VB.Net tag, I can read C# reasonably well, so please feel free to post in either language.
Thanks in advance!
Pete
(Assuming it's even possible in .Net, of course.)
Ultimately, what I want to acplish is an AJAX-based messaging system. I already have a database table for messages, and an ASPX to add new messages (and declare old messages no longer relevant).
The current messaging system simply polls the server every 15 seconds, and re-pulls the current message set.
What I am looking to do is: On $(document).ready(), register an ajax callback function that listens for a server-side event (e.g., MessagesUpdated) On table insert/update, fire MessagesUpdated server-side.
This way, whenever the table is updated (or new records added), any clients listening know that new data is available and can re-poll the server then.
Ideally, I'd also like to make the new data available as an event argument (to minimize re-polling the db).
I can find references to something like this in other languages, but I cannot find any actual code examples to get me started.
Assuming this is possible to do via .Net, can anyone help get me started on this?
I'm using the 2.0 Framework. Also while I added a VB.Net tag, I can read C# reasonably well, so please feel free to post in either language.
Thanks in advance!
Pete
Share Improve this question asked Dec 28, 2011 at 18:13 petepete 25.1k4 gold badges40 silver badges52 bronze badges4 Answers
Reset to default 3Take a look into long polling. Basically what it does is set a long timeout period on AJAX requests. The client then waits for the server to respond. This is much more efficient and instantaneous than sending requests every few seconds.
How to do a long polling client in C#?
For modern browsers, you could use websockets to open a continuous connection to the server, through which the server could notify you of a server-side event. In browsers without websockets support, you would need to use long polling as mentioned in mrtsherman's answer.
HTML5 introduced websockets to address this issue, but support is sparse. If you used it you would need a fallback, which would probably be the long polling mrtsherman described.
I doubt it will work with .NET 2.0, but if you can work around that, you should check out SignalR. SignalR is a plete client- and server-side solution with JS on client and ASP.NET on the back end to create these kinds of applications. There's a good summary here.
Quoting directly from that blog post:
On the client
$(function () {
// Proxy created on the fly
var chat = $.connection.chat;
// Declare a function on the chat hub so the server can invoke it
chat.addMessage = function (message) {
$('#messages').append('<li>' + message + '</li>');
};
$("#broadcast").click(function () {
// Call the chat method on the server
chat.send($('#msg').val());
});
// Start the connection
$.connection.hub.start();
});
And on the server
public class Chat : Hub
{
public void Send(string message)
{
// Call the addMessage method on all clients
Clients.addMessage(message);
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744380769a4571417.html
评论列表(0条)