简体   繁体   English

实时服务器到客户端的通信asp.net C#

[英]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. 我有一个在c#asp.net Web窗体应用程序中的应用程序。 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. 客户端正在更新页面1,假设一个页面具有文本框和网格视图。 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. 用户在文本框中输入一些文本,提交后将其保存在SQL Server数据库中并更新网格视图。 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. 同时,客户端2在第1页上显示。当数据库更新时,我需要让服务器将更新推送到第二个客户端。 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. 我正在寻找不需要JavaScript且可以从服务器推送并在客户端2上的c#中调用函数背后的代码的解决方案。

Performance is a concern because there is potentially high use on the SQL sever and high level of users. 性能是一个令人关注的问题,因为SQL服务器上的潜在使用量很高,用户数量很高。 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 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 OnLoad调用C#中的代码隐藏函数,该函数检查db的更改,然后重新绑定网格

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. 如果没有Javascript保持开放的套接字,尽管对网页的持续轮询至少会刷新,但实际上无法将更改推回客户端。 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. 使用Javascript,您可以进行持久的AJAX调用(请使用超时和错误处理),并在调用最终成功时更新网页。

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. 例如,如果所有内容都在同一台Web服务器上运行,则可以在每次将数据库更新到共享内存时简单地编写一个时间戳,然后在轮询网页并从数据库中重新加载时检查该共享内存。时间戳已更新。 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. 我鼓励您放弃有关“禁止使用javascript”的规则,如果实时页面对客户完全有价值,他们可以将您的服务列入白名单以允许使用javascript。 And continually polling of the entire web page is uglier than a butt pimple. 而且,对整个网页进行持续轮询比对接丘疹更为丑陋。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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