1

This is not a specific coding question, but more of a best practices advice to a newcomer into the web apps world with years of experience in five-nines telecom software development.

I have an ASP.NET web application that collects data from several embedded (Arduino) devices. The device push data periodically via simple HTTP GET requests. Under normal conditions, each device pushes data once every 30 seconds. However, there is a chance that a device could be lost or connectivity from the device to the server could be lost. I need to detect this condition within 2-3 normal data reporting cycles.

If it was a typical stand-alone C# application, I would start a timer every time I receive data from a device with the expiration set to the maximum timeout duration. If the timer expires, this means I have not heard from the device for too long and I need to trigger an alert (email or something) from my application.

So, the inactivity timer approach seems to be pretty typical for stand-alone always-on applications with non-persistent connections. But what is the right way of doing this in an ASP.NET web app? Can I have timers of this kind running outside of my HTTP request handlers?

I searched for possible solutions, and people seem to have more questions about timing out browser sessions and such. My problem is different in these aspects:

  1. I have a very light-weight client incapable of session state tracking
  2. I need to detect inactivity in a timely fashion to alert/alarm the user, who is not interacting with the system until there is an alarm

Thank you for sharing the wisdom.

4

1 回答 1

1

我的建议是,如果您首先使用 get 向服务器添加数据,请切换到 post。其次,尝试使用某种令牌。用户发出的第一个请求(获取请求)应该是他们可以获得访问令牌的端点。然后每次他们通过 post 连接以推送数据时,从标头中获取访问令牌。检查令牌是否已过期。如果很好,请处理他们的请求并使用新的访问令牌进行响应。如果访问令牌已过期,请让他们转到端点以获取新的访问令牌。

对于访问令牌,您可以将其设置为在一定时间后过期。你会做这样的事情:

var timeDiff = DateTime.UtcNow - AccessToken.DateTime;
if (timeDiff.Minutes > 3)  // expired
else                       // still good

这是你会做的事情,尽管获取访问令牌需要更多的代码。

编辑

javascript 有一条interval指令可以在单独的线程上循环并延迟一段时间。你可以让它运行检查访问令牌,看看它是否会过期。当有大约一分钟(或其他时间)到期时,提示用户。在服务器端也有一些东西仍然是最好的做法

于 2014-07-27T18:23:41.297 回答