简体   繁体   中英

real time server to client communication asp.net c#

Hello I am researching some solutions to the following problem any advice would be appreciated.

I have an application that is in c# asp.net web forms application. I need to be able to do real time updates between clients here is the scenario:

Client one is updating page 1 let's say a page that has a text box and a grid view. The user is enters some text in the text box and when submitted it saves in a SQL server db and updates the grid view. At the same time client two is look at page 1 I need to have the server push the update to the second client when the db is updated. I am looking for a solution that does not require JavaScript and can possibly push from the server and call a code behind function in c# on client 2.

Performance is a concern because there is potentially high use on the SQL sever and high level of users. So I would prefer not to poll the db from every client.

Thanks for your help. Any suggestions would be great.

Update: here is my solution for now:

html

       $(function() {
                var updater = $.connection.updateHub;
                //Create broadcast function
                updater.client.broadcastMessage = function () {

                    __doPostBack("upNotes", "");

                };

                //Start the connection to the hub
                $.connection.hub.start().done(function() {
                    $(<%= btnNotesSubmit.ClientID %>).click(function () {
                        updater.server.send();
                    });
                });
            });

<asp:UpdatePanel ID="upNotes" runat="server" UpdateMode="Conditional" OnLoad="ReloadNotes">
   <ContentTemplate>
       <asp:GridView ID="gvNotes" runat="server">

ect...

OnLoad calls a codebehind function in C# that checks the db for changes and then rebinds the grid

Well, you may not be too successful.

Without Javascript keeping an open socket, there is no way to actually push changes back to the client though continual polling of a web page will at least refresh. With Javascript, you can make a long-lasting AJAX call (with timeout and error handling please), and updating the web page when the call finally succeeds.

That web does does not necessary have to hit the database each time it is polled though.

If you don't want to involve the database each time the page is polled, you can either signal a pending change via a different method, or you can have the database update something that can be check without "hitting the database".

For example, if everything is running on the same web server, you can simple write a timestamp whenever you at the same time you update the database to shared memory, and then examine that shared memory when you poll the webpage and reload from the database when the timestamp is updated. I am not recommending this method per se, just giving a simple example.

I would encourage you to drop the rule about "no javascript", if you real-time page is valuable to the customer at all, they can whitelist your service to allow javascript. And continually polling of the entire web page is uglier than a butt pimple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM