检查 ASP.NET 中的连接是否处于活动状态

发布于 2024-07-16 16:49:37 字数 647 浏览 1 评论 0原文

我正在开发一个用 ASP.NET 编写的 Comet 应用程序。 有一系列活动连接上下文 (HttpContext )。 并且有一个线程应该定期迭代集合并检查它们的状态。 所以应用程序架构不是每个请求一个线程。

检查连接是否处于活动状态(未被远程主机关闭)的最佳方法是什么?

我发现这个:

context.Response.Write(' ');
context.Response.Flush();

if (!context.Response.IsClientConnected)
{
    // ...
}

但这不是一个好的解决方案,因为它需要一个线程时间来处理(Flush() 是阻塞操作)。 我需要一种能够非常快速地处理许多并发连接并且不使用阻塞操作的解决方案。

也许有一些 IIS 或 ASP.NET 功能允许以这种方式监视连接?

I work on a Comet application written in ASP.NET. There is an array of active connection contexts (HttpContext). And there is a thread that should periodically iterate through the collection and check theirs state. So application architecture is not thread-per-request.

What is the best way to check that a connection is active (not closed by the remote host)?

I found this:

context.Response.Write(' ');
context.Response.Flush();

if (!context.Response.IsClientConnected)
{
    // ...
}

But it's not a good solution because it takes a thread time to process (Flush() is blocking operation). I need solution that works very fast for many concurrent connections and don't use blocking operations.

Maybe there is some IIS or ASP.NET functionality that allows to monitor connections this way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘过的浮云 2024-07-23 16:49:37

最简洁的答案是不。 ASP.NET 知道为 IsClientConnected 返回 false 的唯一方法是用户对服务器进行不同的调用,或者先前已调用 Response.Close。 根据我的经验,用户很有可能关闭连接,但 IsClientConnected 仍然返回 true。 只要想想正在发生的底层 HTTP 通信,您就会明白原因。

你想达到什么目的?

The short answer is... no. The only way ASP.NET knows to return false for IsClientConnected is if the user makes a different call to the server, or Response.Close has been previously called. It's quite possible for the user to close the connection and still have IsClientConnected return true, in my experience. Just think about the underlying HTTP communication that's going on and you can see why.

What are you trying to accomplish?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文