简体   繁体   English

加载页面后将内容推送到客户端

[英]Push content to client after page is loaded

I wonder if the following is possible: 我想知道以下是否可行:

I have a dll that I have referenced in my web site. 我有一个我在我的网站上引用的DLL。 This dll makes a remote socket connection. 这个DLL进行远程套接字连接。 The socket connection is waiting in the background and the dll reports back data through events after the socket has received some data. 套接字连接在后台等待,并且在套接字收到一些数据后,dll通过事件报告数据。

The connection is opened during load of page. 在加载页面期间打开连接。

Now, I would, for example, like to update a label on the page when new data has arrived. 现在,我想,例如,当新数据到达时,我想更新页面上的标签。

I am not sure how this would work. 我不确定这是如何工作的。 I assume that I could set some kind of timer on page that updates a control but it does not seem "optimal" as I already call code behind through my events. 我假设我可以在页面上设置一些更新控件的计时器,但它似乎并不“最佳”,因为我已经通过我的事件调用了代码。 "Optimally" the UpdatePanel or whatever updates the interface would wait for events and "update" when events has occurred and not based on time. “最佳”UpdatePanel或接口等待事件的任何更新,并在事件发生时“更新”而不是基于时间。

My question is - is this possible? 我的问题是 - 这可能吗?

You can use techniques called "long pulling" or "web sockets". 您可以使用称为“长拉”或“网络套接字”的技术。

There are libraries, like SignalR that can help you. SignalR这样的库可以帮助你。

They generally use web sockets when client's browser supports and long pulling when not. 它们通常在客户端的浏览器支持时使用Web套接字,而在不支持时长时间使用。

Using these libraries you can "push" commands/data from server to client's browser, just as you want. 使用这些库,您可以根据需要将命令/数据从服务器“推送”到客户端的浏览器。

You should think about turning this around; 你应该考虑转过身来; rather than initiate the call from the code behind asynchronously and then update the client, you should deliver your original page to the client. 而不是异步启动后面的代码调用,然后更新客户端,您应该将原始页面传递给客户端。

Once the page has been loaded client side, you can make an AJAX call to the server to retrieve the data you want - and display a little "I'm loading" symbol while this happens. 页面加载到客户端后,您可以对服务器进行AJAX调用以检索所需的数据 - 并在发生这种情况时显示一点“我正在加载”符号。

Page Methods are ideal for this. 页面方法是理想的。

The classic ASP.Net web page is supposed to last for the duration of the users request. 经典的ASP.Net网页应该持续用户请求的持续时间。 When a postback happens, it effectively rebuilds the state of the call from the user's viewstate, session and any other state you've maintained. 发生回发时,它会有效地从用户的viewstate,session和您维护的任何其他状态重建调用状态。 It never cares if the user goes away, for example (although it might feel a bit lonely). 例如,它从不关心用户是否离开(虽然它可能感觉有点孤独)。

Having it hang around for longer is problematic in a number of ways, however, you can implement client callbacks . 将它挂起更长时间在很多方面都存在问题,但是,您可以实现客户端回调 However, although the server initiates this, the client manages the lifecycle, so it's analogous to page methods. 但是,虽然服务器启动了这个,但客户端管理生命周期,因此它类似于页面方法。

Server cannot push the client, this is the rule how web works. 服务器无法推送客户端,这是Web工作的规则。 There are two possible ways to complete your task. 有两种方法可以完成您的任务。

  1. Put timer on the page and make requests on server too see, if something changed 如果发生了变化,将定时器放在页面上并在服务器上发出请求
  2. Open long pulling connection between client and server, like facebook does. 打开客户端和服务器之间的长连接,就像facebook一样。 That listens 听了
    to the events and gets data from the server. 到事件并从服务器获取数据。 It can be, for example xmpp or any other. 它可以是,例如xmpp或任何其他。

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

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