从服务器触发的事件更新页面

时间:2022-08-09 17:03:28

I'm developing this project for a device (let's say) 'Prototype X' and it has manufacturer provided .Net API. This API fires events (which I should subscribe to) upon receiving some special -signal- from the device in real world. What I want/asked to do is, update the asp page (which the user sees) with proper information when this event is fired. By update I mean also refreshing user's page if possible at all...

我正在为一个设备(比方说)'Prototype X'开发这个项目,它有制造商提供的.Net API。在从现实世界中的设备接收到一些特殊信号时,此API会触发事件(我应该订阅)。我想要/要求做的是,在触发此事件时,使用适当的信息更新asp页面(用户看到的)。通过更新我的意思是,如果可能的话,也刷新用户的页面...

How do I do this in asp.net?

我如何在asp.net中执行此操作?

4 个解决方案

#1


3  

You cannot trigger a page update from the server side. What you must do is use a timer on the client side (javascript) to refresh the page (or get the new data via AJAX) on a regular interval.

您无法从服务器端触发页面更新。你必须做的是在客户端使用计时器(javascript)定期刷新页面(或通过AJAX获取新数据)。

#2


1  

You can force the page to refresh by using a meta tag, or javascript. Or AJAX.

您可以使用元标记或javascript强制页面刷新。或者AJAX。

#3


1  

Web aps follow a request-response architecture so you can't really (well, easily) have server initiated messages from client to server.

Web aps遵循请求 - 响应体系结构,因此您无法(很好地,轻松地)从客户端到服务器发送服务器启动的消息。

You can use a polling architecture. At its very simplest, you could simply have the page refresh every X seconds/minutes to check for changes. This may be a bit ugly.

您可以使用轮询架构。最简单的方法是,您可以每隔X秒/分钟刷新页面以检查更改。这可能有点难看。

A more user friendly approach might be to use the XMLHTTP object to poll for changes every few seconds and refresh the page when a change is detected. You could create an HTTP Handler on the server side that simply have the date/time of last change and have a javascript check this every few seconds and when a change occurrs, refresh the page.

更加用户友好的方法可能是使用XMLHTTP对象每隔几秒轮询一次更改,并在检测到更改时刷新页面。您可以在服务器端创建一个HTTP处理程序,该处理程序只具有上次更改的日期/时间,并且每隔几秒就进行一次javascript检查,并在发生更改时刷新页面。

#4


0  

You've got another problem here that has not been addressed. If you've got more than one copy of this device, then how will you associate each device with the user or users who are using it or monitoring it?

你还有另一个问题没有得到解决。如果您拥有此设备的多个副本,那么您将如何将每个设备与正在使用它或监控它的用户或用户相关联?

I suggest you write the "device-access" part of this system in a Windows Service. Let it maintain connection to the devices, keep track of their state, respond to events, etc. Let it maintain a set of data about each device, either in-memory or in a database.

我建议你在Windows服务中编写这个系统的“设备访问”部分。让它保持与设备的连接,跟踪它们的状态,响应事件等。让它维护一组关于每个设备的数据,无论是在内存中还是在数据库中。

The web application could communicate with the service through a WCF service hosted in the Windows Service. There could be one WCF call that asks if anything has changed, and another call to get the changes. The first could be called relatively frequently, through AJAX or meta refresh. The latter would only be called when a change had been seen.

Web应用程序可以通过Windows服务中托管的WCF服务与服务进行通信。可能有一个WCF调用询问是否有任何更改,另一个调用来获取更改。第一个可以通过AJAX或元刷新相对频繁地调用。只有在看到变化时才会调用后者。

#1


3  

You cannot trigger a page update from the server side. What you must do is use a timer on the client side (javascript) to refresh the page (or get the new data via AJAX) on a regular interval.

您无法从服务器端触发页面更新。你必须做的是在客户端使用计时器(javascript)定期刷新页面(或通过AJAX获取新数据)。

#2


1  

You can force the page to refresh by using a meta tag, or javascript. Or AJAX.

您可以使用元标记或javascript强制页面刷新。或者AJAX。

#3


1  

Web aps follow a request-response architecture so you can't really (well, easily) have server initiated messages from client to server.

Web aps遵循请求 - 响应体系结构,因此您无法(很好地,轻松地)从客户端到服务器发送服务器启动的消息。

You can use a polling architecture. At its very simplest, you could simply have the page refresh every X seconds/minutes to check for changes. This may be a bit ugly.

您可以使用轮询架构。最简单的方法是,您可以每隔X秒/分钟刷新页面以检查更改。这可能有点难看。

A more user friendly approach might be to use the XMLHTTP object to poll for changes every few seconds and refresh the page when a change is detected. You could create an HTTP Handler on the server side that simply have the date/time of last change and have a javascript check this every few seconds and when a change occurrs, refresh the page.

更加用户友好的方法可能是使用XMLHTTP对象每隔几秒轮询一次更改,并在检测到更改时刷新页面。您可以在服务器端创建一个HTTP处理程序,该处理程序只具有上次更改的日期/时间,并且每隔几秒就进行一次javascript检查,并在发生更改时刷新页面。

#4


0  

You've got another problem here that has not been addressed. If you've got more than one copy of this device, then how will you associate each device with the user or users who are using it or monitoring it?

你还有另一个问题没有得到解决。如果您拥有此设备的多个副本,那么您将如何将每个设备与正在使用它或监控它的用户或用户相关联?

I suggest you write the "device-access" part of this system in a Windows Service. Let it maintain connection to the devices, keep track of their state, respond to events, etc. Let it maintain a set of data about each device, either in-memory or in a database.

我建议你在Windows服务中编写这个系统的“设备访问”部分。让它保持与设备的连接,跟踪它们的状态,响应事件等。让它维护一组关于每个设备的数据,无论是在内存中还是在数据库中。

The web application could communicate with the service through a WCF service hosted in the Windows Service. There could be one WCF call that asks if anything has changed, and another call to get the changes. The first could be called relatively frequently, through AJAX or meta refresh. The latter would only be called when a change had been seen.

Web应用程序可以通过Windows服务中托管的WCF服务与服务进行通信。可能有一个WCF调用询问是否有任何更改,另一个调用来获取更改。第一个可以通过AJAX或元刷新相对频繁地调用。只有在看到变化时才会调用后者。