How to register an event listener on the client side on the server?

(Suppose this is possible even in .Net, of course.)

Ultimately, I want to create an AJAX-based messaging system. I already have a database table for messages, and ASPX for adding new messages (and declaring old messages is no longer relevant).

The current messaging system checks the server every 15 seconds and re-dials the current set of messages.

What I want to do is: In $ (document) .ready () register an ajax callback function that listens for an event on the server side (for example, MessagesUpdated) On the insert / update table, fire MessagesUpdated server side.

Thus, whenever a table is updated (or new records are added), any clients listening will know that new data is available, and can resubmit the server.

Ideally, I would also like to make new data available as an argument to the event (to minimize re-polling db).

I can find links to something similar in other languages, but I can not find any real code examples to get started.

Assuming this is possible to do through .Net, can someone help me get started with this?

I am using the 2.0 Framework. Also, while I added the VB.Net tag, I can read C # quite well, so please feel free to post in any language.

Thanks in advance!

Pete

+4
source share
4 answers

Take a look at a long survey. Basically, it sets a long wait period for AJAX requests. The client then waits for a server response. This is much more efficient and instant than sending requests every few seconds.

How to make a long customer survey in C #?

+3
source

For modern browsers, you can use websockets to open a continuous connection to the server, with which the server can notify you about this server event. On browsers without websockets support, you will need to use a lengthy poll, as indicated by mrtsherman's answer.

+1
source

HTML5 introduced Web sites to solve this problem, but support is sparse. If you used it, you will need a reserve, which will probably be described by a long survey of mrtsherman.

0
source

I doubt it will work with .NET 2.0, but if you can get around this, you should check SignalR . SignalR is a comprehensive client and server solution with JS on the client and ASP.NET on the back panel for creating such applications. There's a good summary here .

Quote directly from this 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); } } 
0
source

Source: https://habr.com/ru/post/1388347/


All Articles